如何使用javascript为元素设置内联css样式?

时间:2013-10-17 04:16:19

标签: javascript jquery css

我有一个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一个?

5 个答案:

答案 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; 

Css String

在jquery中

$("#idname" or ".classname").css("property":"value");

答案 2 :(得分:0)

在jquery中

用于单个属性设置

$("#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");
相关问题