Javascript获取元素值 [英] Javascript Get Element Value
本文介绍了Javascript获取元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// HTML
< input id =theId value =testonclick =doSomething(this.id,this.value)/>
// Javascript
函数doSomething(id,value){
// ...
}
或
// HTML
< input id =theIdvalue =testonclick =doSomething(this.id)/>
// Javascript
函数doSomething(id){
var value = document.getElementById(id).value;
// ...
}
解决方案
是的,最值得注意的是我不认为第二个会起作用(并且如果是这样,不会很便携)。
// HTML
// Javascript
函数(elem){
var value = elem.value;
var id = elem.id;
...
}
同样可以工作
Is there any difference between these to solutions?
// HTML
<input id="theId" value="test" onclick="doSomething(this.id, this.value)" />
// Javascript
function doSomething(id, value){
// ...
}
or
// HTML
<input id="theId" value="test" onclick="doSomething(this.id)" />
// Javascript
function doSomething(id){
var value = document.getElementById(id).value;
// ...
}
解决方案
Yes, most notably I don't think the second one will work (and if it does, not very portably). The first one should be ok.
// HTML
<input id="theId" value="test" onclick="doSomething(this)" />
// Javascript
function(elem){
var value = elem.value;
var id = elem.id;
...
}
Should also work
这篇关于Javascript获取元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文