为什么这个jQuery点击功能不工作? [英] Why is this jQuery click function not working?

查看:219
本文介绍了为什么这个jQuery点击功能不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程式码:

<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>

上述代码不工作。当我点击#clicker,它不警报,它不隐藏。我检查控制台,我没有得到任何错误。我也检查了JQuery是否加载,确实是。所以不知道什么问题是。我也做了一个文件准备好的函数与警报,工作,所以不知道我做错了什么。请帮忙。

The above code doesn't work. When I click on #clicker, it doesn't alert and and it doesn't hide. I checked the console and I get no errors. I also checked to see if JQuery was loading and indeed it is. 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!

推荐答案

您应该在 $(document).ready

You are supposed to add the javascript code in a $(document).ready(function() {}); block.

$(document).ready(function() {
  $("#clicker").click(function () {
    alert("Hello!");
    $(".hide_div").hide();
  });
});

作为 jQuery文档声明:在文档准备就绪之前,无法安全地操作页面。jQuery检测到这种准备状态。 $(document).ready()只会在页面文档对象模型(DOM)准备好执行

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点击功能不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆