简体   繁体   English

jQuery添加多个具有相同名称的css属性不起作用

[英]jQuery adding multiple css properties with same name doesn't work

I came across a strange issue with jQuery. 我遇到了一个与jQuery有关的奇怪问题。 It seems that when jQuery is used to add multiple css properties with the same name (for cross browser compatibility), each "duplicate" property, is being overwritten and only the last occurrence is being used. 似乎当jQuery用于添加具有相同名称的多个css属性时(对于跨浏览器兼容性),每个“重复”属性都被覆盖,并且仅使用最后一次出现。

Example, in pure css, I have this: 例如,在纯css中,我有这个:

div.ellipse {
    background-image: radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);
    background-image: -o-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);
    background-image: -ms-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);
    background-image: -moz-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);
    background-image: -webkit-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);
}

Fiddle: http://jsfiddle.net/Lzhcdr2f/ 小提琴: http//jsfiddle.net/Lzhcdr2f/

The background image property is used multiple times for cross browser compatibility. 背景图像属性多次使用以实现跨浏览器兼容性。

Now I try to apply the above css code using jQuery like this: 现在我尝试使用jQuery应用上面的css代码,如下所示:

$('.ellipse').css({ 
    'background-image': 'radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image': '-o-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image': '-ms-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image': '-moz-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image': '-webkit-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'
});

Fiddle: http://jsfiddle.net/z9ygxj9j/ 小提琴: http//jsfiddle.net/z9ygxj9j/

The above code will ONLY work in webkit browsers (chrome/safari), because the last line refers to -webkit browser, and jQuery seems to override properties with the same name and only use the last occurrence. 上面的代码只能在webkit浏览器(chrome / safari)中使用,因为最后一行是指-webkit浏览器,而jQuery似乎覆盖了具有相同名称的属性,并且仅使用最后一次出现。

The only way around, seems to be this: 唯一的方法,似乎是这样:

$('.ellipse').css({'background-image': 'radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'});
$('.ellipse').css({'background-image': '-ms-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'});
$('.ellipse').css({'background-image': '-moz-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'});
$('.ellipse').css({'background-image': '-webkit-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'});
$('.ellipse').css({'background-image': '-o-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'});

Fiddle: http://jsfiddle.net/cte7ov36/ 小提琴: http//jsfiddle.net/cte7ov36/

Is there no way to use the same property multiple times inside the same array? 有没有办法在同一个数组中多次使用相同的属性?

The easiest way to maintain this code would be to store your css in a class and use in your elements. 维护此代码的最简单方法是将css存储在类中并在元素中使用。 Style definition should be separate to your code. 样式定义应与您的代码分开。

if you really want to do this in jQuery you can use the attr style 如果你真的想在jQuery中这样做,你可以使用attr风格

var style = [
    'background-image: radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image: -o-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image: -ms-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image: -moz-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)',
    'background-image: -webkit-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)'
].join(';');

$('.ellipse').attr('style', style);

http://jsfiddle.net/z9ygxj9j/2/ http://jsfiddle.net/z9ygxj9j/2/

This is possible by other way 这可以通过其他方式实现

[js fiddle][1]:http://jsfiddle.net/cte7ov36/2/

$('.ellipse').attr('style','background-image: radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);background-image: -ms-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);background-image: -moz-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);background-image: -webkit-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800);background-image: -o-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)');

Note, 注意,

calling .css() 调用.css()

ellipse
.css("background-image", "-[vendorPrefix]-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)");

should not affect non [vendorPrefix] browser display , rendering ; 不应该影响非[vendorPrefix]浏览器显示,渲染; save for possibly opera, which not certain if still utilizes both -o- and -webkit- ? 除了可能的歌剧,不确定是否仍然使用-o--webkit-

This approach checks for a property of element.style ; 这种方法检查element.styleproperty ; replaces given text values within style element text with vendor prefixed value text. 使用供应商前缀值文本替换style元素文本中的给定文本值。

Try 尝试

 var ellipse = $('.ellipse') , style = $("style") , prefixes = { "MozAnimation": "-moz-", "webkitAnimation": "-webkit-", "msAnimation": "-ms-", "oAnimation": "-o-" }; // should not affect non `-moz-` browser ellipse .css("background-image", "-moz-radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800)"); $.each(prefixes, function(key, val) { if (key in ellipse[0].style) { $("style").text(function(i, text) { return text.replace(/(radial-gradient)/g, val + "$1") }) } }); console.log(style.text(), ellipse.css("backgroundImage")); 
 .demo-wrapper div { width: 910px; height: 250px; padding: 10px; margin: 25px auto; border: 5px solid #fff; } .ellipse { background-image: radial-gradient(center bottom, ellipse cover, #ffeda3, #ffc800); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="demo-wrapper"> <div class="ellipse"> <h3>Elliptical Gradient</h3> Lorem Ipsum</div> </div> 

jsfiddle http://jsfiddle.net/cte7ov36/3/ jsfiddle http://jsfiddle.net/cte7ov36/3/

As you said, you try to appear styles generated by the user with some application. 正如您所说,您尝试使用某些应用程序显示用户生成的样式。 We don't know what is exact format of the styles you get from that program element so it is hard to create good answer. 我们不知道从该程序元素获得的样式的确切格式是什么,因此很难创建好的答案。

The answer in the comment by Patrick seems to be useful (just instead of transform use radial-gradient). Patrick的评论中的答案似乎很有用(而不是变换使用径向渐变)。 If you gets all styles together like a block of text then use Filipes answer. 如果你像一块文字一样得到所有的样式,那么使用Filipes的答案。 Also you can detect browser firstly and that apply only that styles that you need (there is example how you can do that) . 此外,您可以首先检测浏览器,并且仅应用您需要的样式(例如,如何执行此操作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM