Javascript HTML 使用变量显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11753778/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:58:44 来源:igfitidea点击:
HTML displaying text using variable
提问by Abhi
I am creating a checkbox in HTML like this:
我正在 HTML 中创建一个复选框,如下所示:
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
Now, I want I have a bike
part i.e. the text that comes besides a checkbox to come from a variable. How can this be done?
现在,我希望I have a bike
部分即复选框之外的文本来自变量。如何才能做到这一点?
回答by Jukka K. Korpela
<input type="checkbox" name="vehicle" value="Bike" id="vehicle" />
<label for="vehicle" id="lblVehicle"></label><br />
<script>
var lblBike = 'I have a bike';
document.getElementById('lblVehicle').innerHTML = lblBike;
</script>
回答by yogi
var yourTextVariable = 'I have a cool bike';
document.getElementById('vehicle').innerHTML = yourTextVariable;