Javascript 如何在引导中检测单击show的button类
我有以下元素Javascript 如何在引导中检测单击show的button类,javascript,jquery,css,twitter-bootstrap,Javascript,Jquery,Css,Twitter Bootstrap,我有以下元素 <button type="button" class='launch' data-toggle="modal" data-target="#myModal">Launch modal</button> <button type="button" class='boom' data-toggle="modal" data-target="#myModal"> Boom </button> 如何检查按钮在函数中是否有类启动或动臂? 或
<button type="button" class='launch' data-toggle="modal" data-target="#myModal">Launch modal</button>
<button type="button" class='boom' data-toggle="modal" data-target="#myModal"> Boom </button>
如何检查按钮在函数中是否有类启动或动臂?
或者换句话说,问题是如何在该函数中获取触发此操作的按钮。使用jQuery方法检查该类是否存在
$('#element').hasClass('launch'); // returns true or false;
请尝试下面的代码。请将.myModalbtn
类添加到两个按钮
$('.myModalbtn').on('hidden', function () {
$(this).hasClass('launch')) // if the it has launch class -> returns true else false;
});
您可以为每个按钮添加单独的类,例如
.modalBtn
,并执行以下操作:
$('.modalBtn').click(function() {
if ($(this).hasClass('launch')) showModal($('.launch'));
else if ($(this).hasClass('boom')) showModal($('.boom'));
});
function showModal( $btn ) {
//insert code to show modal (you can find it in the bootstrap javascript docs. Now you have $btn object to use.
}
但是我无法访问myModal中的按钮。我不知道如何获取按钮,我需要获取触发此模式的按钮以显示。这取决于您要检查的按钮。最好使用onclick并将其绑定到按钮上..如下面的答案所示,使用jquery的.hasClass()方法你的意思是什么取决于要检查的按钮?为什么不收听
单击
事件?我认为不可能在显示事件中获取触发器按钮
$('.modalBtn').click(function() {
if ($(this).hasClass('launch')) showModal($('.launch'));
else if ($(this).hasClass('boom')) showModal($('.boom'));
});
function showModal( $btn ) {
//insert code to show modal (you can find it in the bootstrap javascript docs. Now you have $btn object to use.
}