javascript jQuery 美元符号混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6230428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:00:08  来源:igfitidea点击:

jQuery Dollar Sign Confusion

javascriptjqueryeventsonclickdollar-sign

提问by sichinumi

I'm a bit confused regarding the dollar sign in jQuery, and was hoping someone could help me out.

我对 jQuery 中的美元符号有点困惑,希望有人能帮助我。

I have the following function declaration:

我有以下函数声明:

$(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.

出于某种原因,“create-discussion”按钮的第一个函数声明完美无缺;单击时,会出现一个弹出窗口。但是,第二个不起作用,并且当我单击表格行时不会生成弹出窗口。

Is this some nuance in regards to button onClicks versus table row onClicks? Am I missing something stupidly obvious?

这是关于按钮 onClicks 与表格行 onClicks 的一些细微差别吗?我错过了一些愚蠢明显的东西吗?

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.

此外,我认为如果有人解释$(function() {})实际作用会有所帮助,因为我是这样对待它的$(document).ready(),而且我不确定我是否能做到这一点。

回答by Tadeck

A dollar sign ($) is actually an alias for jQueryfunction. 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.

美元符号 ( $) 实际上是jQuery函数的别名。并且根据文档,如果您将回调作为参数传递给此函数,它将在 DOM 就绪时执行

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.

当涉及到问题的第二部分时(关于为什么代码的第二部分不起作用):只需检查选择器。对我来说,它运行良好(参见jsfiddle- 它没有.button()方法,因为我没有加载 jQuery UI),所以这可能是由不正确的选择器引起的。

回答by CraigW

What you are doing should work so long as your selector text "#listitems tr" has something valid to select.

只要您的选择器文本“#listitems tr”有一些有效的选择,您正在做的事情就应该有效。

You can test by doing ... if the result is 0 that means jQuery did not find any valid items

您可以通过执行...进行测试,如果结果为 0,则表示 jQuery 未找到任何有效项目

alert($("#listitems tr").length);

When you make the call

当您拨打电话时

$("a").click(function(evt) { alert("hello world!"); });

You are binding a click event to all <a> tags on your page. You could very well do the same with ...

您正在将点击事件绑定到页面上的所有 <a> 标记。你也可以用...做同样的事情

$("a").click(myFunc);
function myFunc(evt) { alert("hello world!"); }

A click function can be attached to any html element. It does not have to be a button, it could be , , and etc. The element does not even have to be visible though you will only be able to fire the click if you call the actual event by doing ...

单击功能可以附加到任何 html 元素。它不必是一个按钮,它可以是 , ,等等。该元素甚至不必是可见的,尽管如果您通过执行以下操作调用实际事件,您将只能触发单击...

$("a").click();

$("a").click();

The $("...") is just short hand for typing jQuery("...")

$("...") 只是输入 jQuery("...") 的简写

Hopefully this answers your questions.

希望这能回答您的问题。