(不确定,以防万一)
局限性:使用CSSOM,样式表应来自同一来源。
function getElementChildrenAndStyles(selector) { var html = $(selector).outerHTML(); selector = selector.split(",").map(function(subselector){ return subselector + "," + subselector + " *"; }).join(","); elts = $(selector); var rulesUsed = []; // main part: walking through all declared style rules // and checking, whether it is applied to some element sheets = document.styleSheets; for(var c = 0; c < sheets.length; c++) { var rules = sheets[c].rules || sheets[c].cssRules; for(var r = 0; r < rules.length; r++) { var selectorText = rules[r].selectorText; var matchedElts = $(selectorText); for (var i = 0; i < elts.length; i++) { if (matchedElts.index(elts[i]) != -1) { rulesUsed.push(rules[r]); break; } } } } var style = rulesUsed.map(function(cssRule){ if (cssRule.style) { var cssText = cssRule.style.cssText.toLowerCase(); } else { var cssText = cssRule.cssText; } // some beautifying of css return cssText.replace(/({|;)s+/g, "n ").replace(/As+}/, "}"); // set indent for css here ^ }).join("n"); return "<style>n" + style + "n</style>nn" + html;}
用法:
getElementChildrenAndStyles("#divId");
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)