【问题标题】:OnClick event does not fire on HTML button when clicked on the content of the button单击按钮的内容时,不会在 HTML 按钮上触发 OnClick 事件
【发布时间】:2018-04-23 18:29:39
【问题描述】:

我的 HTML 按钮的 onClick 事件仅在我单击按钮本身时触发,而不是在我单击按钮内的文本或图像时触发 - 例如,如果单击按钮的下部灰色部分,则事件将触发,如果单击文本“Legenda”,则不会触发该事件。

如何通过单击按钮内的文本或图像来触发此事件?

HTML:

<button onclick="dropdown()" class="ui-button ui-widget ui-corner-all dropbtn"><img class="legendaicon" src="images/legend-01.png"><span style="vertical-align:super">Legenda</span></button>
  <div id="myDropdown" class="dropdown-content">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  </div>
</div>

CSS:

#legenda {
right: 0.5%;
margin-top: 0.5%;
z-index: 2000;
position: absolute;

}

.legendaicon {
    height: 25px;
    margin-right: 5px;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f1f1f1;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    z-index: 1;
    float: right;
}

.dropdown-content p {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    right: 0;
}

.show {
    display: block;
}

JS:

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function dropdown() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function (event) {
    if (!event.target.matches('.dropbtn')) {

        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
            }
        }
    }
}

查看JSFiddle中的代码。

【问题讨论】:

    标签: button onclick


    【解决方案1】:

    当你点击按钮内的文字或图片时,你实际上调用了window.onclick内的js函数,这将删除div的类。您可以为按钮添加一个ID并添加一个事件监听器

    function dropdown() {
        document.getElementById("myDropdown").classList.toggle("show", true);
    }
    
    window.addEventListener('click', function(e){   
      if (!document.getElementById('btnID').contains(e.target)){
        if (!event.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
                var openDropdown = dropdowns[i];
                if (openDropdown.classList.contains('show')) {
                   openDropdown.classList.remove('show');
                }
            }
        }
      }
    });
    #legenda {
    right: 0.5%;
    margin-top: 0.5%;
    z-index: 2000;
    position: absolute;
    
    }
    
    .legendaicon {
        height: 25px;
        margin-right: 5px;
    }
    
    .dropdown-content {
        display: none;
        position: absolute;
        background-color: #f1f1f1;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        z-index: 1;
        float: right;
    }
    
    .dropdown-content p {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
        right: 0;
    }
    
    .show {
        display: block;
    }
    <button id="btnID" onclick="dropdown()" class="ui-button ui-widget ui-corner-all dropbtn"><img class="legendaicon" src="images/legend-01.png"><span style="vertical-align:super">Legenda</span></button>
      <div id="myDropdown" class="dropdown-content">
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
      </div>
    </div>

    【讨论】:

    • 感谢您的简单修复!
    猜你喜欢
    • 2020-02-20
    • 2012-01-19
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2016-04-25
    • 2012-09-12
    • 2019-06-22
    相关资源
    最近更新 更多