简体   繁体   English

单击后显示多个按钮的文本

[英]Displaying text for multiple buttons once clicked

I have three buttons that, once clicked, are supposed to show some text.我有三个按钮,一旦单击,应该会显示一些文本。 I tried doing that in javascript but it only showed me the text for the first button underneath it no matter which button I clicked.我尝试在 javascript 中执行此操作,但无论我单击哪个按钮,它都只向我显示其下方第一个按钮的文本。 Here is the code I have for javascript and html:这是我为 javascript 和 html 提供的代码:

 function toggleText() { var text = document.getElementById("demo"); if (text.style.display === "none") { text.style.display = "block"; } else { text.style.display = "none"; } }
 <div class="row"> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" onclick="toggleText()" class="btn btn-sm animated-button thar-two">Button 1</a> <p id="demo" style="display: none">TEXT TEXT TEXT</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" onclick="toggleText()" class="btn btn-sm animated-button thar-two">Button 2</a> <p id="demo" style="display: none">TEXT 2 TEXT 2 TEXT 2</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" onclick="toggleText()" class="btn btn-sm animated-button thar-two">Button 3</a> <p id="demo" style="display: none">TEXT 3 TEXT 3 TEXT 3</p> </div> </div>

I'm still learning javascript so I need help changing the code.我仍在学习 javascript 所以我需要帮助更改代码。 Thanks a lot in advance!提前非常感谢!

First of all the the attribute id should be unique in a document, you can use class attribute instead.首先属性id在文档中应该是唯一的,您可以使用class属性代替。

You can pass this object to the function so that you can refer the respective elements on clicking.您可以this object 传递给 function,以便您可以在单击时参考相应的元素。

Demo:演示:

 function toggleText(el) { var text = el.closest('div').querySelector(".demo"); if (text.style.display === "none") { text.style.display = "block"; } else { text.style.display = "none"; } }
 <div class="row"> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" onclick="toggleText(this)" class="btn btn-sm animated-button thar-two">Button 1</a> <p class="demo" style="display: none">TEXT TEXT TEXT</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" onclick="toggleText(this)" class="btn btn-sm animated-button thar-two">Button 2</a> <p class="demo" style="display: none">TEXT 2 TEXT 2 TEXT 2</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" onclick="toggleText(this)" class="btn btn-sm animated-button thar-two">Button 3</a> <p class="demo" style="display: none">TEXT 3 TEXT 3 TEXT 3</p> </div> </div>

Though, I suggest you to avoid inline event handler, you can use EventTarget.addEventListener() to attach events:不过,我建议您避免使用内联事件处理程序,您可以使用EventTarget.addEventListener()附加事件:

 function toggleText() { var text = this.closest('div').querySelector(".demo"); if (text.style.display === "none") { text.style.display = "block"; } else { text.style.display = "none"; } } var btnLinks = document.querySelectorAll('a.animated-button.thar-two'); btnLinks.forEach(function(btn){ btn.addEventListener('click', toggleText); });
 <div class="row"> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" class="btn btn-sm animated-button thar-two">Button 1</a> <p class="demo" style="display: none">TEXT TEXT TEXT</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" class="btn btn-sm animated-button thar-two">Button 2</a> <p class="demo" style="display: none">TEXT 2 TEXT 2 TEXT 2</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="javascript:void();" class="btn btn-sm animated-button thar-two">Button 3</a> <p class="demo" style="display: none">TEXT 3 TEXT 3 TEXT 3</p> </div> </div>

This an alternative example from other answers, there are so many ways Js can get about doing things.这是其他答案的另一个示例,Js 可以通过多种方式来做事。

  1. There can be only one ID per item and cannot be assigned to something else on parallel.每个项目只能有一个 ID,并且不能并行分配给其他项目。 So id="demo" needs updating.所以 id="demo" 需要更新。
  2. There are many ways to go about this in JS you don;t necessarily need ID on paragraphs, depending upon the which design pattern you adopt. go 有很多方法可以在 JS 中解决这个问题;不一定需要段落上的 ID,具体取决于您采用哪种设计模式。

Notes are in the code example as to what we are doing.代码示例中有关于我们正在做什么的注释。

 //first we get whole list of buttoms present in page var buttonList = document.querySelectorAll('a'); //then we add event listner of all of the buttons no need for inlineJs for (i = 0; i < buttonList.length; i++) { buttonList[i].addEventListener("click", function(event){ //first after click we cancel any default event of achor tag event.preventDefault() //detect which button was clicked var buttonclicked = event.target; //get its immediate next parapragh elemetn var texts = buttonclicked.nextElementSibling; //get paragraphs actuall display property var displaylevel = texts.style.display; //run your if else here if(displaylevel =='none'){ texts.style.display = 'block'; } else {texts.style.display = 'none';} }); }
 <div class="row"> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="#" class="btn btn-sm animated-button thar-two">Button 1</a> <p style="display: none">TEXT TEXT TEXT</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="#" class="btn btn-sm animated-button thar-two">Button 2</a> <p style="display: none">TEXT 2 TEXT 2 TEXT 2</p> </div> <div class="col-md-3 col-sm-3 col-xs-6"> <a href="#" class="btn btn-sm animated-button thar-two">Button 3</a> <p style="display: none">TEXT 3 TEXT 3 TEXT 3</p> </div> </div>

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

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