简体   繁体   English

按钮 onclick 的奇怪行为

[英]Strange behavior of button onclick

Im currently working on a project and I have some trouble with my navbar.我目前正在做一个项目,我的导航栏遇到了一些问题。

I have two buttons there in a dropdown:我在下拉列表中有两个按钮:

<div class="dropdown-menu" aria-labelledby="navbarDropdown">
   <button class="dropdown-item all" id = "my_all" onclick = "showApplied()">Angemeldet</button>
   <button class="dropdown-item off" id = "empty" onclick = "switchShowAllEmpty()">Leere Aktivitäten einblenden</button>
</div>

The second button works fine, but when I click the first one nothing happens.第二个按钮工作正常,但是当我单击第一个按钮时没有任何反应。 When I change the over button "empty" still is working and button "my_all" not.当我更改结束按钮“empty”时仍然有效,而按钮“my_all”没有。 BUT when I manipulate the copy inside the browser by copying the first button and pasting it in the dropdown again, the pasted one works although the origional didn't.但是,当我通过复制第一个按钮并将其再次粘贴到下拉列表中来操作浏览器内的副本时,尽管原始按钮没有,但粘贴的按钮仍然有效。

To my this just makes no sense.对我来说,这毫无意义。 And the onclick-function also works fine, so this is not the problem.而且 onclick 功能也可以正常工作,所以这不是问题。 Please let me know if someone has an idea what's the problem.如果有人知道问题出在哪里,请告诉我。

The problem in your code lies at line number #143 inside showAll() function.您的代码中的问题在于 showAll() function 内的第 143 行。

function showAll(){
        document.getElementById("my_all").onclick = "showApplied()";

You are trying to give a string value (which is function text) to onclick property rather than a function definition.您正在尝试为 onclick 属性而不是 function 定义提供字符串值(即 function 文本)。 You can remove that because anyways you have applied onclick event to the button in HTML.您可以删除它,因为无论如何您已将 onclick 事件应用于 HTML 中的按钮。

Anyway if you really want it there then,无论如何,如果你真的想要它,

You can try document.getElementById("my_all").onclick = showApplied你可以试试document.getElementById("my_all").onclick = showApplied

You can refer onclick property docs here .您可以在此处参考 onclick 属性文档。 You can also try an see working example here .您也可以在此处尝试查看工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM