vi.*_*su. 5 jquery twitter-bootstrap
我正在使用 twitter bootstrap modal 和 jquery。
当有多个按钮可以创建模式对话框时,如何找到源按钮?
在 bootstrap-modal 之前,我使用 onclick 来显示确认消息,例如 onclick="newGame(3, true)"; 现在我想用 bootstrap-modal 来做到这一点。
html,
<button type="button" class="btn" id="ctrl6" data-controls-modal="my-modal" data-
backdrop="static">3</button>
<button type="button" class="btn" id="ctrl7" data-controls-modal="my-modal" data-
backdrop="static">4</button>
<button type="button" class="btn" id="ctrl8" data-controls-modal="my-modal" data-
backdrop="static">5</button>
<div id="my-modal" class="modal hide fade in" style="display: none;">
...
</div>
Run Code Online (Sandbox Code Playgroud)
js,
$(document).ready(function() {
// Hide modal if "Go back" is pressed
$('#my-modal .go-back-button').click(function() {
$('#my-modal').modal('hide');
});
// Hide modal if "Okay" is pressed
$('#my-modal .okay-button').click(function() {
$('#my-modal').modal('hide');
});
});
Run Code Online (Sandbox Code Playgroud)
谢谢。
该click函数接受一个链接到触发事件的元素的参数:
$('#my-modal .okay-button').click(function(e) {
// e is the element that triggered the event
console.log(e.target); // outputs an Object that you can then explore with Firebug/developer tool.
// for example e.target.firstChild.wholeText returns the text of the button
});
Run Code Online (Sandbox Code Playgroud)
示例在这里(jsfiddle)。