所以我试着找出答案,但没有成功。
window.onload = function() {
console.log(document.getElementById('#sell'));
if(document.getElementById('#sell')){
alert('jo');
//Init for Vue
}
}
它可以和jQuery一起工作,但是不能和vanilla JS一起工作,为什么?
console.log(document.getElementById('#sell'));
结果返回NULL
。
jQuery示例:
if ( $( "#sell" ).length ) {
const app = new Vue({
el: '#sell',
components: { sellPicture }
});
}
在我的sell.blade.php中看起来是这样的
...
<div id="sell">
<sell-picture></sell-picture>
</div>
....
我的脚本添加在/body之前
这只是个小问题。我需要用于引导的jQuery,如果我不用它来改变DOM,那么在Vue中也使用它是不是很糟糕?
发布于 2018-01-04 18:55:36
从id参数中删除#,如下所示
window.onload = function() {
console.log(document.getElementById('sell'));
if(document.getElementById('sell')){
alert('jo');
//Init for Vue
}
}
发布于 2018-01-04 18:57:08
document.getElementById('#sell')
不需要#
来标识Ids。这是您将在jQuery选择器中使用的语法,但是DOM选择器将从字面上获取字符串并将其与ID属性值进行匹配。对于document.getElementByClassName()
也是如此,您不需要使用.
来标识类。
Mozilla MDN文档给出了此here的一个示例。
这应该是可行的:
window.onload = function() {
console.log(document.getElementById('sell'));
if(document.getElementById('sell')){
alert('jo');
//Init for Vue
}
}
发布于 2018-01-04 18:57:50
请尝试使用
$(document).ready(function(){ console.log($("#sell")); });
https://stackoverflow.com/questions/48093691
复制相似问题