$.(“”).css(“”, “”);
That is not good, but a few lines of code corrected the situation.getStyles
), rules for property-value ( setStyle
), and apply to the loaded document.$.fn.extend({
setStyle : function (selector, rulesString) {
var rules = (rulesString + ';' ).match( /([^:]+)\:([^\;]*)\;/gi );
for ( var id in rules) {
rules[id].replace( /[\s]*([^\s]+)[\s]*\:([^\;]*)\;/gi ,
function (rule, propery, value) {
$(selector).css(propery, value)
})
}
},
getStyles : function (styleRef){
var styles = styleRef.replace( /\/\*(.|\s)*?\*\//gi , "" , "$1" ).match( /[^\}]*\{([^\}]*)\}/gi );
for ( var id in styles) {
styles[id].replace( /\s+/gi , " " , "$1" ).replace( /([^\{]*)\{([^\}]+)\}/gi ,
function (style, selector, rules){
$.fn.setStyle(selector, rules)
});
}
},
_css : function (styleRef) {
if ( /.*\.css/gi .test(styleRef)) {
$.ajax({
type : "GET" ,
url : styleRef,
success : function (retValue) {
$.fn.getStyles(retValue)
},
error : false
})
} else if ( /[^\}]*\{([^\}]*)\}/gi .test(styleRef)) {
$.fn.getStyles(styleRef)
} else {
$.fn.setStyle( this .selector, styleRef)
}
return true
}
})
$.extend({
_css : function (styleRef) {
return $.fn._css(styleRef)
}
})
Colored with dumpz.org$._css("../css/style.css")
;$._css("div {color2: red; font-size: 16px;}");
or so $("p")._css("font-weight: bold; color: green;");
style.css
, we style.css
it with the script and without it, measuring time using the Chrome developer tools. We measure the work of the script by sending messages to the console like var currentDate = new Date(); console.log(" " + currentDate.getTime() + "ms");
var currentDate = new Date(); console.log(" " + currentDate.getTime() + "ms");
Source: https://habr.com/ru/post/114913/
All Articles