我有以下元素
<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?
})
如何检查按钮是否在功能内部有类启动或繁荣? 或换句话说,问题是我如何获得在此功能中触发此操作的按钮。
答案 0 :(得分: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;
});
答案 1 :(得分: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.
}