我有一个div我正在显示一个按钮图像。我实际上正在显示一个精灵。我在标签中添加了一个onlick属性,它调用了一个名为ACFunction的函数。我知道如何使用javascript设置带有css属性的style属性,但我需要在下面的每个属性中设置style属性,除了我将它们设置为略有不同的值,这些值将允许我想要的图像单击按钮时显示,显示。
所以现在,这是按钮的当前内联样式
style="width:74px; height:99px; background:url(images/icons.png) -0px -0px;"
我可以使用:
document.getElementById('id').style.
对于这些属性中的每一个?我该如何设置background:url
一个?
答案 0 :(得分:3)
<强>的jQuery 强>
$("#id").css({"width":"74px", "height":"99px",
"background":"url(images/icons.png) -0px -0px"});
答案 1 :(得分:3)
是的,你可以使用
document.getElementById('id').style.property = value;
也喜欢这个
document.getElementById("demo").setAttribute(
"style", "font-size: 100px; font-style: italic; color:#ff0000;");
即便如此
document.getElementById("myElement").style.cssText = cssString;
在jquery中
$("#idname" or ".classname").css("property":"value");
答案 2 :(得分:0)
用于单个属性设置
$("#idname").css("background":"desired setting");// for background setting
表示多个属性
$("#idname").css({"width":"74px",
"height":"99px",
"background":"url(images/icons.png) -0px -0px"
});
参考 css
答案 3 :(得分:0)
是的,您可以使用document.getElementById('id').style.
使用像background: url('');
答案 4 :(得分:0)
试试这个:
* Define all styles with the classname "myStyles".
* $("#id").addClass("myStyles");