如果我在jquery中的attr()方法中给了attr(,)多个样式,为什么attr(()会应用最后一个重复的样式



我使用了attr((在元素上应用样式,我给出了4个样式属性,但它正在应用最后一个样式属性。所以我想知道为什么它只应用最后一种样式,原因是什么?

<!DOCTYPE html>
<html>
<body>
<p id="prg1">first paragraph</p>
<h1 id="h11">first heading</h1>
<h1 id="h22">second heading</h1>
</body>
<script src="C:UsersSUDARSHANDesktophtml_UIjquery-3.6.0.js">
</script>
<script>
$('document').ready(function (){
$('#prg1').attr({style:'color:yellow',style:' font-family:arial',style: 'border-style:dotted'})
})
</script>
</html>

如果您想在样式中应用多个选项,可以这样做:

$('#prg1').attr({
style: 'color:yellow;font-family:arial;border-style:dotted'
})

演示

$('document').ready(function() {
$('#prg1').attr({
style: 'color:yellow;font-family:arial;border-style:dotted'
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="prg1">first paragraph</p>
<h1 id="h11">first heading</h1>
<h1 id="h22">second heading</h1>

$('document').ready(function() {
$('#prg1').css({'color:yellow;font-family:arial;border-style:dotted'})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="prg1">first paragraph</p>
<h1 id="h11">first heading</h1>
<h1 id="h22">second heading</h1>

最新更新