Javascript 如何使用javascript使按钮标签动态化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13717816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make button label dynamic using javascript?
提问by Casa_De_Agua
What I want to do is be able to change the text inside of a button by making the text inside an actual variable.
我想要做的是能够通过将文本置于实际变量中来更改按钮内的文本。
Something like this:
像这样的东西:
<button type=button>status</button>
but instead of a string, it's a variable:
但它不是一个字符串,而是一个变量:
var status = 'on';
回答by trebuchet
If you're trying to assign the text of a button to a variable then it's as simple as following:
如果您尝试将按钮的文本分配给变量,那么它很简单,如下所示:
var buttonText = "click me!";
document.getElementById("id-of-your-button").innerHTML = buttonText;
回答by Sudeep
if you are using jquery, then its a simple task.
如果您使用的是 jquery,那么它是一项简单的任务。
suppose you have a button
假设你有一个按钮
then you can use jquery to assing its text by using
然后你可以使用 jquery 来通过使用来分配它的文本
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript">
var status="on";
$(document).ready(function(){
$("#myButton").text(val);
});
</script>
or you could pure javascript:
或者你可以使用纯 javascript:
var status="on";
document.getElementById('myButton').innerHTML=status;
回答by Akhil Sekharan
Guess this helps:
猜猜这有帮助:
<button id="myButton" type="button" value=""></button>
<input id="myInputButton" type="button" value=""></button>
<script type="text/javascript">
var status = 'on';
document.getElementById('myButton').innerHTML = status;
document.getElementById('myInputButton').value = status;
</script>

