jQuery美元符号混乱 [英] jQuery Dollar Sign Confusion
问题描述
我对jQuery中的美元符号感到困惑,希望有人可以帮助我。
I'm a bit confused regarding the dollar sign in jQuery, and was hoping someone could help me out.
我有以下函数声明:
$(function() {
$( "#create-discussion" ).button().click(function() {
alert("Clicked");
});
$( "#listitems tr" ).click(function(event) {
alert("clicked");
});
});
由于某种原因,创建讨论按钮的第一个函数声明工作正常;当点击时,弹出窗口出现。但是,第二个不起作用,当我点击表行时,不会生成弹出窗口。
For some reason, the first function declaration for the "create-discussion" button works perfectly; when clicked, a popup appears. However, the second one does not work, and no popup is generated when I click on the table rows.
这是关于按钮onClicks与表行onClicks的一些细微差别?我是否缺少一些愚蠢的东西?
Is this some nuance in regards to button onClicks versus table row onClicks? Am I missing something stupidly obvious?
另外,如果有人解释了什么 $(function(){})
实际上是,因为我正在处理它像 $(document).ready()
,我不知道我能不能这样做。
Also, I think it would help a bunch if someone explained what $(function() {})
actually does, as I'm treating it like $(document).ready()
, and I'm not sure if I can do that.
推荐答案
美元符号( $
)实际上是 jQuery
函数。而根据文档,如果您将回调作为参数传递给此函数,它将在DOM准备就绪时执行。
A dollar sign ($
) is actually an alias for jQuery
function. And according to the documentation, if you pass a callback as an argument to this function, it will be executed when the DOM is ready.
当您的问题的第二部分(关于代码的第二部分为什么不工作):只需检查选择器。对我来说,它的工作正常(参见 jsfiddle - 它没有 .button()
方法,因为我没有加载jQuery UI),所以这可能是由错误的选择器引起的。
When it comes to the second part of your question (about why the second part of the code is not working): just check the selectors. For me it is working perfectly (see jsfiddle - it is without .button()
method, because I am not loading jQuery UI), so this may be caused by incorrect selectors.
这篇关于jQuery美元符号混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!