我必须使用javascript对象绑定HTML字段。 这是我的HTML -
<div>
<img src="value1"><br/>
<p>value2</p>
<a href="value3"/>
</div>
我在javascript函数中有value1
,value2
,value3
。如何将此值绑定到html字段?
答案 0 :(得分:2)
如果你想在HTML中改变你想要在HTML中更改的值,最好是在javascript中创建这些元素。但如果我只是按照你的HTML,你可以使用:
(你的html中有一些标记错误,所以我改变了)
HTML
<div><img id="value1" src="#" /><br/>
<p id="value2">value2</p>
<a id="value3" href="#">Link</a></div>
脚本
// here you give value to the js variables
var value1 = 'image.jpg';
var value2 = 'Test text';
var value3 = 'http://www.stackoverflow.com';
// here you put/bind the values with the ids in the html
document.getElementById('value1').src = value1;
document.getElementById('value2').innerHTML = value2;
document.getElementById('value3').href= value3
答案 1 :(得分:-1)
例如:
<div><img id="image" src=value1><br/>
<p id="paragraph">value2</p>
<a id="link" href=value3/></div>
document.getElementById('image').src = value1;
document.getElementById('paragraph').innerHtml = value2;
document.getElementById('link').href=value3
答案 2 :(得分:-1)
像document.getElementById('image')一样绑定.src = value1;