[英]jQuery code won't run when index.html is opened in browser. no errors in console
I just can't get a very simple toggleClass() to work and I can't figure out why. 我只是无法使用非常简单的toggleClass()来工作,而且我也不知道为什么。 It's actually just an exercise to get started with jQuery.
实际上,这只是开始使用jQuery的一种练习。 It works fine here as a snippet and when I enter it manually in the console just not when I open the index.html file from my computer in Chrome or Firefox.
它在这里可以作为一个片段很好地工作,当我在控制台中手动输入它时,仅当我在Chrome或Firefox中从计算机中打开index.html文件时,它才起作用。 When I then enter the jQuery manually in the console, it works, too!
然后,当我在控制台中手动输入jQuery时,它也起作用! The files are all linked properly, I've confirmed via alert.
我已通过警报确认文件已正确链接。 The div appears red and won't change colour when hovered.
div悬停时显示为红色,并且不会更改颜色。 Any suggestions??
有什么建议么??
$('.test').hover( function() { $(this).toggleClass('testhover'); });
.test { width: 100px; height: 100px; background-color: red; } .testhover { background-color: blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"></div>
that is because you insert your script BEFORE the .test element. 那是因为您在.test元素之前插入了脚本。 While the DOM loads, it first executes your script, and then adds div.test.
加载DOM时,它首先执行脚本,然后添加div.test。
<div class="test"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
this will solve it 这将解决它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.