如何应用CSS字符串使用Javascript / jQuery没有解析/标记化检索属性值对? [英] How to apply CSS string using Javascript/jQuery without parsing/tokenizing to retrieve property-value pairs?
问题描述
我有一些CSS字符串形式,例如
I have some CSS in string form, e.g.
border: solid 1px #0000ff; background-color: #ffff00;
并且我想将该CSS应用于< div& code>。但是使用jQuery来应用CSS的所有例子都涉及使用
css
方法,为了走这条路线,我必须用分号(;
)检索属性值对,然后通过(:
)检索属性和值, >
and I want to apply that CSS to a <div>
. But all of the examples for using jQuery to apply CSS involve using the css
method, and to go that route, I'd have to split the CSS string by semicolons (;
) to retrieve property-value pairs, then by (:
) to retrieve the property and value, and do
$('myDiv').css(property, value);
有没有办法直接从字符串形式应用CSS?
我担心如果CSS值可以包含这些字符,那么CSS(分号和冒号)的简单解析将会失败,例如 url
子值。
I worry that such a naive parsing of the CSS (semicolons and colons) will fail if CSS values can contain those characters, e.g. in url
sub-values.
推荐答案
c $ c> style 属性,然后设置一个新的:
You can retrieve the existing style
attribute and then set a new one:
var target = $("#target");
target.attr("style", target.attr("style") + "; " + yourString);
这篇关于如何应用CSS字符串使用Javascript / jQuery没有解析/标记化检索属性值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!