访问多个css属性

时间:2013-03-07 16:14:30

标签: javascript jquery

此方法是否适合访问多个css方法?

<script>
$("div").click(function () {
  var html = ["The clicked div has the following styles:"];

  var styleProps = $(this).css( ["width", "height", "color", "background-color"] );

这就是jquery API正在做的事情。上面的方法是否适合访问多个属性?或者就是这样?

  $.each( styleProps, function( prop, value ) {
    html.push( prop + ": " + value );
  });

 $( "#result" ).html( html.join( "<br>" ) );

});

1 个答案:

答案 0 :(得分:0)

您复制了docs中的代码,提到它可能会有所帮助,无论如何,他们会向您显示他们提取的所有数据:

var styleProps = $(this).css( ["width", "height", "color", "background-color"] );

现在他们用以下方法操纵数据:

 $.each( styleProps, function( prop, value ) {
    html.push( prop + ": " + value );
  });

最后,他们在每个名称 - 值对之间输出<br>的结果:

$( "#result" ).html( html.join( "<br>" ) );