Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

javascript - 为什么这个jQuery click函数不起作用?(Why is this jQuery click function not working?)

Code:(码:)

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <script type="text/javascript"> $("#clicker").click(function () { alert("Hello!"); $(".hide_div").hide(); }); </script> The above code doesn't work.(上面的代码不起作用。) When I click on #clicker, it doesn't alert and and it doesn't hide.(当我单击#clicker时,它不会发出警报,也不会隐藏。) I checked the console and I get no errors.(我检查了控制台,没有任何错误。) I also checked to see if JQuery was loading and indeed it is.(我还检查了JQuery是否正在加载,实际上是否正在加载。) So not sure what the issue is.(所以不确定是什么问题。) I also did a document ready function with an alert and that worked so not sure what I am doing wrong.(我还执行了带有警报的文档就绪功能,并且该功能正常工作,因此无法确定我在做什么错。) Please help.(请帮忙。) Thanks!(谢谢!)   ask by starbucks translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are supposed to add the javascript code in a $(document).ready(function() {});(您应该将JavaScript代码添加到$(document).ready(function() {});)

block.(块。) ie(即) $(document).ready(function() { $("#clicker").click(function () { alert("Hello!"); $(".hide_div").hide(); }); }); As jQuery documentation states: "A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute"(正如jQuery文档指出的那样:“在文档“就绪”之前,无法安全地操纵页面。jQuery会为您检测到此就绪状态。 $( document ).ready()包含的代码仅在页面Document Object运行一次模型(DOM)已准备就绪,可以执行JavaScript代码”)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...