且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当包含在单独的文件中时,为什么jQuery单击不起作用

更新时间:2023-11-12 08:36:58

点击功能需要使用$(function(){...}).它告诉jquery文档已加载,并且可以开始在DOM上工作.在此之前,DOM还没有准备好,您的元素可能不存在.

You need to have $(function() { ... }) for your click function to work. It tells jquery that the document is loaded and it can start working on the DOM. Before then, the DOM is not ready and your element might not exist.

编辑:也可以将其用于外部文件,除非您更高级并且知道何时不使用它.

Use this for your external files too, unless you are more advanced and know when NOT to use it.