-1

我有以下元素

<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>

我有一个事件监听器:

$('#myModal').on('show', function () {   

       // how do I check if button has class launch or boom? 
})

如何检查按钮在函数内是否有类启动或繁荣?或者换句话说,问题是我如何在这个函数中获得触发这个动作的按钮。

4

2 回答 2

0

您可以为每个按钮添加一个单独的类.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.



}
于 2013-04-24T06:24:32.020 回答
0

使用 jQuery .hasClass方法检查类是否存在。

$('#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;
});
于 2013-04-24T05:39:02.350 回答