的ID如何識別模態窗口的ID在這個JS識別模式窗口
$('.modal-Goalkeepers, .modal-Defenders, .modal-Midfielders, .modal-Forwards').on('show.bs.modal', function (data) {
var id = {{here I want to know id of modal is now shown}}
});
的ID如何識別模態窗口的ID在這個JS識別模式窗口
$('.modal-Goalkeepers, .modal-Defenders, .modal-Midfielders, .modal-Forwards').on('show.bs.modal', function (data) {
var id = {{here I want to know id of modal is now shown}}
});
你可以試試這個:
$('.modal-Goalkeepers, .modal-Defenders, .modal-Midfielders, .modal-Forwards').on('show.bs.modal', function(data) {
var id = $(this).attr('class');
});
一個例子:
$('.block1,.block2,.block3').click(function() {
alert($(this).attr('class'));
});
.block1,.block2,.block3 {
height:100px;
margin:20px;
background:#000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="block1"></div>
<div class="block2"></div>
<div class="block3"></div>
更新:他需要得到類,而不是ID!
我收到'undefined'( –
你確定他們有和id? –
我想收到'modal-Goalkeepers'或'modal-Defenders'或者另一個 –
由於您使用的是jQuery,因此您可以通過關鍵字'this'來訪問ID。 這將訪問該事件正在調用的元素。
$(this).attr('id');
這是不工作的模態窗口。我收到'未定義'。 –
你能告訴我們您正在使用的HTML,並通過替換類名的關鍵字ID,因爲它是混亂給大家 –
使用ID的,因爲他們是唯一的和有限的,每一個元素之一。您正在尋找的解決方案可能會或可能不會每次都提供一致的結果。 –
我的錯。抱歉。 –