javascript 多个具有相同#id 的按钮来执行相同的功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27440757/
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
Multiple buttons with the same #id to do the same function?
提问by common
JS beginner, sorry
JS初学者,不好意思
How can could I make it so that every button that has the id "#popit" to open the same popup box?
我怎样才能让每个具有“#popit”ID 的按钮打开同一个弹出框?
I'm using bPopup
我正在使用bPopup
With this code there is only one button on the site which does open the popup
使用此代码,网站上只有一个按钮可以打开弹出窗口
;(function($) {
$(function() {
$('#my-button').bind('click', function(e) {
e.preventDefault();
$('#element_to_pop_up').bPopup();
});
});
})(jQuery);
http://jsfiddle.net/yg5so25s/- there are 3 buttons with the same id, but only the first one opens the popup box, anyway I could make it so that every single button to open the same popupbox?
http://jsfiddle.net/yg5so25s/- 有 3 个按钮具有相同的 id,但只有第一个打开弹出框,无论如何我可以让每个按钮打开同一个弹出框?
回答by Resolution
id
must be unique, you need to use class instead:
id
必须是唯一的,您需要改用类:
<button class="my-button">POP IT UP</button>
then you can use .
to target elements by class name:
然后您可以使用.
按类名定位元素:
;(function($) {
$(function() {
$('.my-button').bind('click', function(e) {
e.preventDefault();
$('#element_to_pop_up').bPopup();
});
});
})(jQuery);