【发布时间】:2017-06-20 14:11:50
【问题描述】:
两天前我问了一个关于堆栈溢出的问题。 How can I get all the properties from the class or id on click using jQuery?
我只想知道为什么这段代码在 Firefox、Safari 和 Edge 中不能正常工作,我该如何解决。
$(".sample").click(function() {
var html = [],
x = $(this).css([
"margin", "padding", "color", "border"
]);
$.each(x, function(prop, value) {
html.push(prop + ": " + value);
});
$("#result").html(html.join("<br>"));
})
关于代码:
代码用于获取元素的css属性。
问题:
正如我上面所描述的,问题是由@wazz 报告的,谢谢。
请查看给定链接中的评论部分。
先谢谢了。
更新:这是我的完整代码
$(".sample").click(function() {
var html = [],
x = $(this).css([
"margin", "padding", "color", "border"
]);
$.each(x, function(prop, value) {
html.push(prop + ": " + value);
});
$("#result").html(html.join("<br>"));
})
.sample {
margin: 10px;
color: #000;
padding: 5px;
border: 1px solid #4073ff;
}
#test {
background: url(../sample.jpg) 20px 20px no-repeat cover red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>
<button class="sample">Click</button>
【问题讨论】:
-
我们应该检查哪个链接?
-
@CarstenLøvboAndersen 抱歉,我忘了提供链接。我已经编辑了我的问题
-
请提供HTML,可能有几个原因。
-
@llya Kantor 当然可以。请稍等。
-
@llyaKantor 我已经更新了我的问题
标签: javascript jquery css firefox safari