我使用了以下脚本:
script type="text/javascript">
$(document).ready(function () {
$('li:last-child').hover(function () {
$('#test').addClass('transparent'); //mouseover
}, function () {
$('#test').removeClass('transparent'); //mouseout
});
});
</script>
使用以下 HTML 布局:
<body onload="javascript that makes the menu...">
<div id="menu" //this houses the li elements that are created by the javascript></div>
<div id="test">
本质上,我有 javascript 创建一个充满 li 元素的菜单。这些元素中的最后一个应该能够在悬停时使用 id 测试更改 div 的不透明度。相关的css如下:
.transparent{
opacity: 0 !important;
}
但是由于某些原因,这段代码不起作用。在浏览器的控制台中检查给我一个 TypeError,在上面看到的启动 javascript 的行中 $(...) 为空。什么可能导致此错误,我该如何解决?
最佳答案
也许您的 JQuery 库没有被加载或者存在冲突。您是否尝试过使用 jQuery(document) 而不是 $(document)?它有相同的行为吗?如果是的话,我建议你看看你的图书馆的路径,如果它是正确的。
关于javascript - 什么会导致 document.ready 函数为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27808924/