Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

JavaScript Toggle button not working for buttons in an HTML table

The following JavaScript code toggles HTML buttons. However buttons in a <table> element are not toggled, instead the buttons flash the label "Forget" for a split second. Why are <button> elements in each cell of a table not toggled by the JavaScript code below?

<button class="buttonOne">Connect</button>
<button class="buttonTwo">Connect</button>
<button class="buttonThree">Connect</button>
question from:https://stackoverflow.com/questions/65846419/javascript-toggle-button-not-working-for-buttons-in-an-html-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could select them by giving unique Id. Example:

   <button id="btn1" class="buttonOne">Connect</button>
   <button id="btn2" class="buttonTwo">Connect</button>
   <button id="btn3" class="buttonThree">Connect</button> 

And select them at your Js file:

const btn1 = document.getElementById('btn1');
const btn2 = document.getElementById('btn2');
const btn3 = document.getElementById('btn3');

This way you should have no problems with those buttons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...