在HTML我有一些标签输入标记后替换文本标签:之前或输入标签
<label>Address <input type="text" name="address" /></label>
<label><input type="text" name="surname" /> Surname</label>
我想更改标签文本与一个javascript funcion。
我知道这是可能的,例如。与
input_name.parentNode.firstChild.nodeValue = "New label text";
不幸的标签是输入前后。所以firstChild以及lastChild。
我知道可以将标签放入<span>
并使用getElementById。但我不喜欢。
也许还可以将id
或for
添加到标签...但我不喜欢。
如何轻松地替换“输入之前或之后的第一个TextNode兄弟”?
谢谢,非常好。 为什么严格等于'nodeType === 3'而不是'== 3'? – Salvador 2012-03-09 11:00:32
查看关于'=='和'==='的回答http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – 2012-03-09 14:23:43