使用HTML中的按钮更改文本

时间:2018-04-24 10:41:54

标签: javascript html css

我有一个大约8个按钮的列表,列出了不同类别的商店(男装,食品,体育等),我想在点击它们时显示每个类别的商店列表,但使用相同的页面区域。所以它将取代体育用品店'与'食品店'当我点击"食品商店"按钮。

我怎么能这么简单地做到这一点? (我是一个菜鸟,如果可以的话,尽量保持简单)

2 个答案:

答案 0 :(得分:5)

您可以尝试使用这个简单的javascript来更改文本onclick

 <div id="chgtext">This is my current text</div>
    <a href="#" onclick="document.getElementById('chgtext').innerHTML='Change the text using javascript';">Change text</a> &nbsp; &nbsp;
    <a href="#" onclick="document.getElementById('chgtext').innerHTML='Text will be changed based on the other text';">Change text2</a>&nbsp;  &nbsp;
    <a href="#" onclick="document.getElementById('chgtext').innerHTML='This is another sample changed text on click the onther text';">Change text3</a>&nbsp;  &nbsp;
    </div>

答案 1 :(得分:0)

上面的答案可以完成工作,但是这里有一个如何以一种可重复使用的方式执行此操作的示例,因此您没有重复的document.getElementById('chgtext').innerHTML=代码。

这也向您显示onclick属性将允许您运行或调用JavaScript代码。

&#13;
&#13;
function changeText(text) {

  document.getElementById('chgtext').innerHTML = text;

}
&#13;
<div id="chgtext">This is my current text</div>

<a href="#" onclick="changeText('Change the text using javascript')">Change text</a> &nbsp; &nbsp;
<a href="#" onclick="changeText('Text will be changed based on the other text')">Change text2</a>&nbsp;  &nbsp;
<a href="#" onclick="changeText('This is another sample changed text on click the onther text')">Change text3</a>&nbsp;  &nbsp;
&#13;
&#13;
&#13;

相关问题