我有一个有多个bootstrap模式的html视图。每个模态都有一个前缀为静态名称的id,后缀为图像的名称,该图像只是随机字符串(即id="image-0jM5v1sh2tAQ7BVPomZl"
,id="image-0WzpWwCFbjSWD5im7It8"
,id="image-0xnd7RvHOxrLxhCfHupp"
)
我希望每次用户点击打开任何这些模态时都会运行一个函数。所以我有这个代码:
$("div[id*='image-']").on('shown.bs.modal', function () {
// How do I assign this image variable a value of the name of the full <div> id?
var image = // I want this to equal for example "image-0WzpWwCFbjSWD5im7It8' but it needs to be dynamic based on which modal the user opened.
})
答案 0 :(得分:0)
$("div[id*='image-']").on('click', function() {
var image = $(this).attr("id").split("-")[1];
alert(image);
})
&#13;
div {
background-color: lightblue;
width: 50px;
height: 50px;
margin: 5px;
float: left;
padding: 5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="image-0jM5v1sh2tAQ7BVPomZl">test1</div>
<div id="image-0WzpWwCFbjSWD5im7It8">test2</div>
<div id="image-0xnd7RvHOxrLxhCfHupp">test3</div>
&#13;