Min*_*hev 17
试试这段代码:
?$('input').val('');
Run Code Online (Sandbox Code Playgroud)
它循环遍历所有input元素,并将其值设置为空字符串.这是一个演示:http://jsfiddle.net/maqVn/1/
当然,如果你没有jQuery:
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i += 1) {
inputs[i].value = '';
}????
Run Code Online (Sandbox Code Playgroud)
由于我更喜欢功能风格,您可以使用:
Array.prototype.slice.call(
document.getElementsByTagName('input'))
.forEach(function (el) {
el.value = '';
});
Run Code Online (Sandbox Code Playgroud)
[].forEach.call(document.getElementsByTagName('input'), function(e){
e.value = '';
});
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想抓住textareas和其他元素,你可以使用querySelectorAll:
[].forEach.call(
document.querySelectorAll('input, textarea'),
function(e) { e.value = ''; }
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20495 次 |
| 最近记录: |