简体   繁体   English

如何使用javascript更改HTML元素的值?

[英]How do you change the value of an HTML element with javascript?

I have this: <input type="button" value="hello"> I want to change the value with javascript so that it is value="goodbye" . 我有这个: <input type="button" value="hello">我想用javascript更改value ,以便它是value="goodbye" How do I do this? 我该怎么做呢?

Following @David's advice in the comments below here is the code I tried but could not get to work before posting this question: 在下面的评论中关注@ David的建议是我尝试的代码,但在发布此问题之前无法开始工作:

var createBttn = document.getElementById('create');
createBttn.innerHTML = 'value="goodbye"';

First you need to get a reference to the object that you want to change the value on, then assign the value property of that element, like this: 首先,您需要获取要更改值的对象的引用,然后分配该元素的value属性,如下所示:

Say your element had an id of "someButton": 假设你的元素的id为“someButton”:

var btn = document.getElementById('someButton');
btn.value = 'goodbye';

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

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