首页 文章

JQuery结束按钮事件处理程序[重复]

提问于
浏览
1

这个问题在这里已有答案:

我有五个带有bootstrap closebuttons ,我将 onclick 事件处理程序添加到 alert() 下一个sibiling文本中 . 问题是它仅在第一个关闭按钮上显示 alert() .

http://jsfiddle.net/4cz57mge/

<script>
    $("#closenotify").click(function(){
        alert($(this).next().text());
    });
</script>

我是JQuery的新手让我知道我在这里缺少什么?

谢谢

1 回答

  • 1

    它's because you'使用一个唯一的ID,所以jQuery在它找到的第一个停止 . 继续并更改您的代码以使用类,它将工作 . 您已经为每个添加了 close 类,因此只需将其更改为以下内容即可 .

    另外,你的jsFiddle中还有一些额外的 div 标签 .

    解决方案

    $(".close").click(function(){
        alert($(this).next().text());
    });
    

    工作演示

    $(".close").click(function(){
        alert($(this).next().text());
    });
    
    .alert-purple { border-color: #694D9F;background: #694D9F;color: #fff; }
    .alert-info-alt { border-color: #B4E1E4;background: #81c7e1;color: #fff; }
    .alert-danger-alt { border-color: #B63E5A;background: #E26868;color: #fff; }
    .alert-warning-alt { border-color: #F3F3EB;background: #E9CEAC;color: #fff; }
    .alert-success-alt { border-color: #19B99A;background: #20A286;color: #fff; }
    .glyphicon { margin-right:10px; }
    .alert a {color: gold;}
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="col-md-12">
        <div class="alert alert-purple alert-dismissable">
            <span class="glyphicon glyphicon-certificate"></span>
            <button type="button" id="closenotify" class="close" data-dismiss="alert" aria-hidden="true">
                ×</button><strong>sagsagsagsagsag</strong> 
        </div>
            </div>
    
      <div class="col-md-12">
        <div class="alert alert-purple alert-dismissable">
            <span class="glyphicon glyphicon-certificate"></span>
            <button type="button" id="closenotify" class="close" data-dismiss="alert" aria-hidden="true">
                ×</button><strong>fdsggsdgsdgsdg</strong> 
        </div>
            </div>
    
      <div class="col-md-12">
        <div class="alert alert-purple alert-dismissable">
            <span class="glyphicon glyphicon-certificate"></span>
            <button type="button" id="closenotify" class="close" data-dismiss="alert" aria-hidden="true">
                ×</button><strong>sgaasgsagsaggasg</strong> 
        </div>
            </div>
    
      <div class="col-md-12">
        <div class="alert alert-purple alert-dismissable">
            <span class="glyphicon glyphicon-certificate"></span>
            <button type="button" id="closenotify" class="close" data-dismiss="alert" aria-hidden="true">
                ×</button><strong>This is a test nigga</strong> 
        </div>
            </div>
    
      <div class="col-md-12">
        <div class="alert alert-purple alert-dismissable">
            <span class="glyphicon glyphicon-certificate"></span>
            <button type="button" id="closenotify" class="close" data-dismiss="alert" aria-hidden="true">
                ×</button><strong>This is a test nigga</strong> 
        </div>
    </div>
    

    文档

    https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id

相关问题