如何使用 jQuery 发出警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2804157/
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
How to alert using jQuery
提问by Phillip Senn
This works:
这有效:
$('.overdue').addClass('alert');
But this doesn't:
但这不会:
$('.overdue').alert('Your book is overdue.');
What is the correct jQuery syntax for:
什么是正确的 jQuery 语法:
FOR EACH CLASS="overdue"
alert('Your book is overdue');
NEXT
回答by Adam Sills
$(".overdue").each( function() {
alert("Your book is overdue.");
});
Note that ".addClass()" works because addClass is a function defined on the jQuery object. You can't just plop any old function on the end of a selector and expect it to work.
请注意,“.addClass()”之所以有效,是因为 addClass 是在 jQuery 对象上定义的函数。您不能只是在选择器的末尾放置任何旧函数并期望它起作用。
Also, probably a bad idea to bombard the user with n popups (where n = the number of books overdue).
此外,用 n 个弹出窗口轰炸用户可能是一个坏主意(其中 n = 过期书籍的数量)。
Perhaps use the size function:
也许使用 size 函数:
alert( "You have " + $(".overdue").size() + " books overdue." );
回答by Sean Vieira
Don't do this, but this is how you would do it:
不要这样做,但这是你的方式:
$(".overdue").each(function() {
alert("Your book is overdue");
});
The reason I say "don't do it" is because nothingis more annoying to users, in my opinion, than repeated pop-ups that cannot be stopped. Instead, just use the length
property and let them know that "You have X books overdue".
我之所以说“不要这样做”,是因为在我看来,没有什么比无法停止的重复弹出窗口更让用户烦恼的了。相反,只需使用该length
属性并让他们知道“您有 X 本书过期”。
回答by Brian Mains
For each works with JQuery as in
对于每个与 JQuery 一起使用的作品
$(<selector>).each(function() {
//this points to item
alert('<msg>');
});
JQuery also, for a popup, has in the UI library a dialog widget: http://jqueryui.com/demos/dialog/
对于弹出窗口,JQuery 在 UI 库中有一个对话框小部件:http: //jqueryui.com/demos/dialog/
Check it out, works really well.
看看,效果真的很好。
HTH.
哈。