javascript - 我想循环遍历数组并修改属性

标签 javascript jquery arrays loops

这是我的代码

var input_buttons = ["#one","#two","#three"]; 
var substr = input_buttons.split(',');
for(var i=0; i< substr.length; i++) 
{
   substr.attr('value', '');
}

为什么这不起作用?

最佳答案

您的第一个问题是在数组上调用 split(',') 。但是,如果您只想将所有这些值设置为空白字符串,您可以这样做:

$('#one,#two,#three').val('');

如果你想设置不同的值,你需要循环:

$('#one,#two,#three').each(function() {
  // this == the HTML node (not a jQuery element)
  this.value = someValue; // someValue would set outside
};

关于javascript - 我想循环遍历数组并修改属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5247788/

相关文章:

c - C 中的递归数组函数

c - 数组将数字列表中的相同数字存储两次

javascript - 为什么 call 比 apply 快得多?

jquery - 为什么我的 JSONP 返回无效的 JSON

javascript - 没有 jQuery 的 WordPress - 这可能吗?

jquery - 如何在iPad上实现dblclick事件

javascript - 一个 div 标签有 z-index :2 how to change sub div tag z-index:3

php - 变量内的数组

Javascript 动画问题

javascript - Internet Explorer 中 'script' 标记的“onload”处理程序