javascript 如何在jQuery中的同一事件上绑定多个id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20118270/
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 bind multiple ids on same event in jQuery?
提问by Kalyan
Here is my HTML code:
这是我的 HTML 代码:
<a style='cursor:pointer' id='anchor1' onmouseover=fnover(this)>
<a style='cursor:pointer' id='anchor2' onmouseover=fnover(this)>
<a style='cursor:pointer' id='anchor3' onmouseover=fnover(this)>
Here is my JavaScript code:
这是我的 JavaScript 代码:
fnover(obj){
$('.dropdown').fadeIn();
}
My requirement is, I have multiple ids, but I want to bind id dynamically using jQuery. Whenever you mouseover on anchor tag that particular anchor tag will be fadeInor fadeOut.
我的要求是,我有多个 id,但我想使用 jQuery 动态绑定 id。每当您将鼠标悬停在锚点标签上时,该特定锚点标签将是fadeIn或fadeOut。
Any suggestion?
有什么建议吗?
回答by rita
$('#id1,#id2,#id3').on('mouseenter',function(){
//do stuff on mouse over
});
$('#id1,#id2,#id3').on('mouseleave',function(){
//do stuff on mouse out
});
easier of course if you just give your items a class instead of a separate ids, as they seem to do the same thing...
当然,如果你只是给你的项目一个类而不是一个单独的 id,那么更容易,因为它们似乎做同样的事情......
$('.myclass').on('mouseleave',function(){
//do stuff on mouse out
});
回答by Satpal
You can pass multiple selector, separated by comma like
您可以传递多个选择器,用逗号分隔,如
$( "#anchor1, #anchor2, #anchor3" ).mouseover(function() {
$('.dropdown').fadeIn();
});
回答by Ishan Jain
Try this :
试试这个 :
onmouseover=fnover($(this).attr("id"))
回答by Mark Walters
Do you mean something like this
你的意思是这样吗
<a style='cursor:pointer' id='anchor1' />
<a style='cursor:pointer' id='anchor2' />
<a style='cursor:pointer' id='anchor3' />
and then bind the same functionality to all three anchors
然后将相同的功能绑定到所有三个锚点
$('#anchor1, #anchor2, #anchor3').hover(function(){ //Select all three
$('.dropdown').fadeIn(); //On mouse over
}, function(){
$('.dropdown').fadeOut(); //on mouse out
});
although this code would be better written by giving all the anchor tags you wish to attach this functionality to a class like aFade
and then selecting them using
尽管通过提供您希望将此功能附加到类的所有锚标记来编写此代码会更好aFade
,然后使用
$('.aFade').hover(function.........
回答by Karthik Chintala
You can do some thing like this if you have some common name for anchor tag id's
如果你有一些锚标签 id 的通用名称,你可以做这样的事情
$(function(){
$("a[id*=anchor]").mouseover(function(){
//do your stuff here
});
});
The above script states that if any anchor tag whose id starts with anchor
, then the event will be wired up. You don't even have to specify all the id's of the anchor tag.
上面的脚本指出,如果任何 id 以 开头的锚标记anchor
,则事件将被连接起来。您甚至不必指定锚标记的所有 id。
Its also better if you have some common .classname
for your anchor tags. So that only that particular class names will wireup the events.
如果你.classname
的锚标签有一些共同点,它也会更好。这样只有特定的类名才会连接事件。
Here is the fiddle
这是小提琴
Hope it helps
希望能帮助到你
回答by J2D8T
Why not add a class to each HTML element and use the getElementByClass() function to select the elements that you want to use the fadeIn() and fadeOut() on.
为什么不向每个 HTML 元素添加一个类,并使用 getElementByClass() 函数来选择要对其使用fadeIn() 和fadeOut() 的元素。