我的网页上有一些javascript代码正在将一些div加载到页面上.我还想为每个div添加onmouseenter和onmouseleave事件处理程序.我使用jquery添加这些处理程序,但我收到错误:
"对象[对象DOMWindow]的属性'$'不是函数"
我的代码看起来像这样,它在for循环中:
var newItem = document.createElement('div');
newItem.innerHTML = results[i];
newItem.setAttribute("id", "resultDiv_" + i.toString());
dropDown.appendChild(newItem);
//Error on next line...
$("resultDiv_" + i.toString()).bind("mouseenter", function() {
$("resultDiv_" + i.toString()).css({ 'background-color': 'blue' });
});
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法,为什么我得到这个错误,甚至错误意味着什么?
尝试更换所有出现$用jQuery.
选择器$("resultDiv_" + i.toString())也不会返回任何元素.你可能意味着:$("#resultDiv_" + i.toString())
最后确保在DOM准备就绪时执行此代码,即内部:
jQuery(function() {
// Put your code here
});
Run Code Online (Sandbox Code Playgroud)
您确定 jQuery 已正确加载吗?会不会与另一个 javascript 库冲突?