编辑:我需要使用一堂课,感谢您指出一个给我

我有以下div:

<div class="section-link" id="section-tooltip" data-content="Popup with option trigger" rel="popover" data-placement="right">
    <div class="section-features" style="display: none;">
    Content
    </div>
</div>
<div class="section-link" id="section-tooltip" data-content="Popup with option trigger" rel="popover" data-placement="right">
    <div class="section-features" style="display: none;">
    Content
    </div>
</div>
<div class="section-link" id="section-tooltip" data-content="Popup with option trigger" rel="popover" data-placement="right">
    <div class="section-features" style="display: none;">
    Content
    </div>
</div>


如您所见,它们都使用相同的ID,我正在尝试使用以下JQuery在引导程序中创建一个弹出窗口,但是仅对一个div执行此操作,而我试图对它们全部进行设置而无需设置个人ID。

$('#section-tooltip').each(function(){
        $(this).popover({
    trigger: "hover",
    html : true,
    content: function() {
      return $(this).children('div:first').html();
    }
    });
});

最佳答案

将ID转换为一个类,然后修改您的代码:

<div class="section-link section-tooltip" data-content="Popup with option trigger" rel="popover" data-placement="right">
    <div class="section-features" style="display: none;">
    Content
    </div>
</div>

$('.section-tooltip').each(function(){
    $(this).popover({
        trigger: "hover",
        html : true,
        content: function() {
            return $(this).children('div:first').html();
        }
    });
});


这是假设您有其他元素使用同一类(.section-link),而您不希望将此行为应用到该元素。否则,请改用该类。

关于javascript - 具有相同ID的多个div的事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17704608/

10-10 00:08
查看更多