此方法适合访问多个 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>" ) );
});
最佳答案
您从 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>" ) );
关于javascript - 访问多个 css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15276141/