点击区域触发模态框时如何获取触发元素ID实现差异化修改
问题根因
HTML规范要求同一页面内id属性必须唯一,你给多个<area>标签设置了相同的id="ModArea",jQuery的id选择器$("#ModArea")只会匹配页面中第一个符合条件的元素,所以点击事件只绑定给了第一个冰岛区域,点击爱尔兰区域时你写的click逻辑根本不会执行,才会出现标题为空或者始终显示冰岛的问题。
解决方案
你不需要给每个区域加唯一id就能实现需求,用class属性绑定公共事件即可,如果确实需要获取唯一id,也可以给每个区域加不同id后从触发事件的元素上读取。
1. 修改HTML代码
把重复的id替换为class,可选给每个区域加唯一id:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- 运行前请确保已引入Bootstrap 4.x及以上版本的CSS和JS文件,否则模态框功能无法正常使用 --> <img class="europe_img" src="https://mapswire.com/download/europe/europe-political-map-miller.jpg" usemap="#europe" alt="欧洲地图"> <map name="europe"> <!-- 把id换成class,可选加唯一id --> <area class="mod-area" id="iceland" target="" alt="冰岛" title="冰岛" coords="138,388,35,540,232,748,549,585,516,374" shape="poly" data-toggle="modal" data-target="#Modal"> <area class="mod-area" id="ireland" target="" alt="爱尔兰" title="爱尔兰" coords="791,1240,644,1337,635,1568,846,1496,866,1304" shape="poly" data-toggle="modal" data-target="#Modal"> <div class="modal fade" id="Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="country_name"></h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> 模态框内容,可根据点击区域动态修改 </div> </div> </div> </div> </map>
2. 修改JS代码
用class选择器绑定事件,可直接读取当前点击元素的所有属性,包括唯一id、title等:
// 方式1:直接绑定click事件 $(".mod-area").click(function() { // 获取当前点击区域的唯一id const areaId = $(this).attr('id'); // 获取title属性 const name = $(this).attr('title'); $("#country_name").html(name); // 还可以根据areaId做更多差异化修改,比如不同国家显示不同的模态框内容 if(areaId === 'iceland') { $('.modal-body').text('这是冰岛的专属内容'); } else if(areaId === 'ireland') { $('.modal-body').text('这是爱尔兰的专属内容'); } }); // 方式2:用bootstrap模态框事件监听,更适配bootstrap原生逻辑 $('#Modal').on('show.bs.modal', function (event) { // 获得触发模态框的元素 const triggerArea = $(event.relatedTarget); const areaId = triggerArea.attr('id'); const name = triggerArea.attr('title'); const modal = $(this); modal.find('#country_name').text(name); // 其他差异化逻辑同上 })
3. 修正CSS问题
你之前的.modal fade选择器写法错误,两个类名之间需要加.连写,否则样式不生效:
/* 修正后的模态框背景样式 */ .modal.fade { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); } /* 其他原有CSS保持不变即可 */ .modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; border: 1px solid #888; width: 80%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); -webkit-animation-name: animatetop; -webkit-animation-duration: 0.4s; animation-name: animatetop; animation-duration: 0.4s } @-webkit-keyframes animatetop { from { top: -300px; opacity: 0 } to { top: 0; opacity: 1 } } @keyframes animatetop { from { top: -300px; opacity: 0 } to { top: 0; opacity: 1 } } .close { color: white; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } .modal-header { padding: 2px 16px; background-color: #5cb85c; color: white; } .modal-body { padding: 2px 16px; }
内容的提问来源于stack exchange,提问作者Anshar
相关产品推荐
相关产品推荐

