简体   繁体   English

如何在不解析/标记化的情况下使用Javascript / jQuery应用CSS字符串来检索属性 - 值对?

[英]How to apply CSS string using Javascript/jQuery without parsing/tokenizing to retrieve property-value pairs?

I have some CSS in string form, eg 我有一些字符串形式的CSS, 例如

border: solid 1px #0000ff; background-color: #ffff00;

and I want to apply that CSS to a <div> . 我想将CSS应用于<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 但所有的使用jQuery应用CSS涉及使用实例css方法,并走这条路,我必须用分号拆分CSS串( ; )来检索属性值对,然后通过( : )至检索属性和值,然后执行

$('myDiv').css(property, value);

Is there a way to apply the CSS directly, from its string form? 有没有办法从字符串形式直接应用CSS?

I worry that such a naive parsing of the CSS (semicolons and colons) will fail if CSS values can contain those characters, eg in url sub-values. 我担心如果CSS值可以包含那些字符, 例如url子值中,那么CSS(分号和冒号)的这种天真解析将会失败。

You can retrieve the existing style attribute and then set a new one: 您可以检索现有的style属性,然后设置一个新属性:

var target = $("#target");
target.attr("style", target.attr("style") + "; " + yourString);

Live Example | 实例 | Source 资源

Assuming there are no inline styles set on the element using the style attribute, the following will work: 假设使用style属性在元素上没有设置内联样式,则以下内容将起作用:

$("#foo").attr('style', 'border: solid 1px #0000ff; background-color: #ffff00;');

Example fiddle 示例小提琴

Note that this is not an ideal solution though. 请注意,这不是一个理想的解决方案。 A much better method would be to put the required styles in a class and use addClass() instead. 一个更好的方法是将所需的样式放在一个类中,然后使用addClass()

First get your div element using a selector: 首先使用选择器获取div元素:

var div = document.getElementById("myDiv"); // myDiv is the id right?

Then get the current css: 然后获取当前的css:

var css = div.getAttribute("style");

If it's null then set it to the new style. 如果它为null则将其设置为新样式。 Otherwise append the new style: 否则追加新风格:

var style = 'border: solid 1px #0000ff; background-color: #ffff00;';
if (css === null) css = style;
else css += style;

Finally update the style of the div: 最后更新div的样式:

div.setAttribute("style", css);

That's it. 而已。 See the demo: http://jsfiddle.net/xUWzw/ 看演示: http//jsfiddle.net/xUWzw/

There is another way of adding string based styles. 还有另一种添加基于字符串的样式的方法。 If below is the style string - 如果下面是样式字符串 -

    border: solid 1px #0000ff; background-color: #ffff00;

Then 然后

    var div = document.getElementById('myDiv');
    div.style.cssText = 'border: solid 1px #0000ff; background-color: #ffff00;';

Fiddle: https://jsfiddle.net/eja0zea4/ 小提琴: https//jsfiddle.net/eja0zea4/

您可以使用jQuery .attr()函数将css字符串直接添加到style属性中,但是更好的方法是使用jQuery .addClass()函数为div分配一个类,并将css属性添加到类。

Why not creating a CSS class and just using the addClass removeClass method from jQuery 为什么不创建一个CSS类,只使用jQuery中的addClass removeClass方法

.myClass {
   border: solid 1px #0000ff; 
   background-color: #ffff00;
}

then 然后

$(div).addClass('myClass');

只需使用jQuery的函数.attr('style', 'somthing...') ,它非常易于使用,您不必考虑容错。

您可以将其作为对象检索

$('myDiv').css({property:"value",property:"value",property:"value"});

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

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