onClick文本更改

时间:2013-06-12 11:28:21

标签: javascript html button text onclick

所以我对html很新,需要一些帮助。

我正在尝试制作3个按钮,每个按钮都会更改单击它们旁边的文本。我为每个按钮使用以下代码:

<a href="#" onClick="document.getElementById('past').innerHTML='---future---';">

当我点击按钮时,文本将在innerHTML =“”之后的文本中更改。问题是我在地方添加了很多文字---未来---它不再在浏览器中加载它。屏幕不会更新。我该如何克服这个问题?我已经有这个问题已经有一段时间了,所以任何帮助都会得到满足。

4 个答案:

答案 0 :(得分:9)

<script>
function changeText()
{
 document.getElementById('boldStuff').innerHTML = 'Fred Flinstone';
}
</script>
<p>Welcome to the site <b id='boldStuff'>dude</b> </p> 
<input type='button' onclick='changeText()' value='Change Text'/>
</body>
</html>

试用此代码。这个对我有用。 在上面的代码中我使用[粗体]标记来集中它你可以使用缩写标签。

答案 1 :(得分:6)

<p id="peep"></p>

<button onclick="myFunction('Harry Potter')">Click for Harry Potter</button>
<button onclick="myFunction('Bob')">Click for Bob</button>

<script>
function myFunction(name)
{
document.getElementById("peep").innerHTML = "Welcome " + name;
}
</script>

在所有按钮上使用相同的功能来更改文本!

选中fiddle

答案 2 :(得分:0)

点击文字时尝试更改文字:

 <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>

以下是demo

答案 3 :(得分:0)

使用此代码:

HTML:

<h1> Welcome to the </h2><span id="future-clicked"></span>

<button onclick="clicked_on()">Click for future</button>

JS:

<script>
    function clicked_on(){
        document.getElementById('future-clicked').innerHTML = 'Future World';
    }