简体   繁体   English

在浏览器中打开index.html时,jQuery代码将无法运行。 控制台中没有错误

[英]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.

相关问题 index.html 不会在 Chrome 浏览器中显示 main.js 代码 - index.html won't show main.js code in chrome browser Heroku不会加载index.html - Heroku won't load index.html Index.html在VScode外打开时不显示页面 - Index.html does not display the page when opened outside VScode Angular 4 index.html不会加载任何脚本 - Angular 4 index.html won't load any script npm run build不起作用。“在file://上打开index.html将不起作用。” - npm run build doesn't work.“Opening index.html over file:// won't work.” 在浏览器上单击 index.html 时,内容未呈现,尽管 CDN 应用于 Sublime 文本 - Contents aren't rendering when index.html is clicked on the browser, though the CDN is applied on Sublime text 为什么在浏览器中打开index.html而不是通过节点服务器提供index.html时,“ this”有何不同? - Why is `this` different when opening index.html in the browser as opposed to serving index.html with a node server? 如果没有 index.html,如何在 index.html 中运行脚本 - How to run scripts in index.html in react if you don't have index.html Webpack Build 生成 index.html 但在浏览器中加载时为空白 - Webpack Build generates index.html but blank when loaded in browser 如果页面是index.html,如何运行jquery脚本 - How to run jquery script if page is index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM