js怎样批量设置元素的style属性值

2025-02-27 03:06:17
推荐回答(3个)
回答1:

1、首先来看下测试代码,html里,主要部分有二个input输入框,和一个按钮。

2、在js代码里,借助Jquery插件库来操作,以减少代码,所以先引入对应的脚本库文件。

3、js代码里,为按钮添加点击事件,点击后,就用jquery设置姓名输入框的disabled属性,设置为false,这样输入框就不能输入了,为禁止状态了。只要调用jquery的 attr方法,然后输入属性的名称和值就行了:$("#name").attr('disabled', true)。

4、运行页面,可以看到现在的输入框是可以输入的,是正常状态的,点击一下“修改”按钮。

5、点击后,输入框就变成了禁止状态了,成功设置了输入框的属性。

回答2:

firefox等可以使用:

document.getElementById("id").setAttribute("style","top:20px;left:20px;color:red;");

IE中则必须使用style.cssText

document.getElementById("id").style.cssText = "top:20px;left:20px;color:red;";

回答3:

var style=
{
    color:"#fcc",
    position:"absolute"
}
for( var i in style)
{
   document.getElementById("id").style[i]=style[i] 
}