单击按钮内容时,OnClick事件不会触发HTML按钮

时间:2018-04-23 09:55:15

标签: button onclick

我的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中的代码。

1 个答案:

答案 0 :(得分:1)

单击按钮内的文本或图像时,实际调用window.onclick中的js函数将删除div的类。您可以为按钮添加ID并添加事件监听器

&#13;
&#13;
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');
            }
        }
    }
  }
});
&#13;
#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;
}
&#13;
<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>
&#13;
&#13;
&#13;