简体   繁体   English

如何使用 Javascript 更改按钮的文本?

[英]How can I change the text of a button with Javascript?

I'm trying to make it so that a simple collapsible disclosure button will trigger a window that will display the disclosure (or in this case filler text) and then hide it again when clicked.我正在尝试让一个简单的可折叠披露按钮触发一个 window ,它将显示披露(或在本例中为填充文本),然后在单击时再次隐藏它。 I've got the collapsed code working for the most part but the part I can't get right is the right-pointing arrow which I'm using Unicode to generate.我的折叠代码大部分都在工作,但我不能正确的部分是我正在使用 Unicode 生成的指向右箭头。 I want it to change to a down-pointing arrow but for some reason, I can't get the script to work.我希望它更改为向下箭头,但由于某种原因,我无法让脚本工作。

I am very new to this and I've been trying to piece together various solutions I've found here but unfortunately, I've hit a roadblock I can't seem to locate.我对此很陌生,我一直在尝试拼凑我在这里找到的各种解决方案,但不幸的是,我遇到了我似乎无法找到的障碍。

My code also throws an error saying that the disclosure collapse function isn't a function even though it's defined and does appear to do it's job.我的代码还抛出一个错误,指出即使它已定义并且似乎确实可以完成工作,但披露崩溃 function 不是 function。 It's just the text change function that does nothing on click.只是文本更改 function 在点击时没有任何作用。

At some point I'd like to swap out the Unicode arrows for chevron svgs since those don't get formatted on mobile devices like the Unicode arrows do but I need to figure out the JS part first before I make this any more complicated.在某些时候,我想将 Unicode 箭头换成雪佛龙 svgs,因为它们不会在 Unicode 箭头等移动设备上格式化,但我需要先弄清楚 JS 部分,然后再让它变得更复杂。

 // Disclosure Display // var acc = document.getElementsByClassName("disclosureAccordionBtn"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function(displayDisclosure) { this.classList.toggle("active"); var disclosurePanel = this.nextElementSibling; if (disclosurePanel.style.display == "block") { disclosurePanel.style.display = "none"; } else { disclosurePanel.style.display = "block"; } }); } // Disclosure Label Text Change Arrow Direction // function disclosureLabelText() { var btn = document.getElementById("disclosureLabel"); if (btn.value == "&#9654;Disclosure") { btn.value = "&#9660;Disclosure"; } else { btn.value = "&#9654;Disclosure"; } }
 <;-- disclosure button --> <input id="disclosureLabel" class="disclosureAccordionBtn" type="button" onclick="displayDisclosure(); disclosureLabelText();" value="&#9654:Disclosure" style="outline; none: border; none: color; #646464: cursor; pointer: text-decoration; none: background-color; transparent: text-align; center: font-size: 11px" /> <;-- disclosure collapse --> <div id="displayDisclosure" class="disclosurePanel" style="display: none; outline: none; border: none; color: #646464; text-decoration: none; background-color: transparent; text-align: center; font-size: 11px; text-align: justify; text-justify: inter-word. margin-left; 0:5%. margin-right; 0:5%. margin-top; -0:7%. margin-bottom, 0,5%">Lorem ipsum dolor sit amet. consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat, Lorem ipsum dolor sit amet. consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </div>

Here's a JSFiddle with everything in it as well:这是一个包含所有内容的 JSFiddle:

https://jsfiddle.net/ts0p94bk/ https://jsfiddle.net/ts0p94bk/

To fix the issue, remove displayDisclosure() from the onclick attribute value.要解决此问题,请从 onclick 属性值中删除 displayDisclosure()。 It works fine after that.之后它工作正常。

In the code displayDisclosure is the callback function parameter when you are adding the event listener to the button.在代码中 displayDisclosure 是当您向按钮添加事件侦听器时回调 function 参数。

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

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