如何使用 javascript 遍历页面上的所有输入框并清除其中的数据(使它们全部为空白)?
最佳答案
试试这段代码:
$('input').val('');
它遍历所有 input
元素并将它们的值设置为空字符串。
这是一个演示:http://jsfiddle.net/maqVn/1/
当然,如果您没有 jQuery:
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i += 1) {
inputs[i].value = '';
}
因为我更喜欢函数式风格,你可以使用:
Array.prototype.slice.call(
document.getElementsByTagName('input'))
.forEach(function (el) {
el.value = '';
});
关于javascript - 遍历所有输入框并清理它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13435231/