gpt4 book ai didi

javascript - jQuery - 如何使用元素的 HTML 获取所有样式/css(在内部/外部文档中定义)

转载 作者:技术小花猫 更新时间:2023-10-29 10:16:08 26 4
gpt4 key购买 nike

我知道 $("#divId").html()会给我 innerHtml。我还需要它的样式(可能通过类的方式定义)或者内联 style一个单独的 <style> 中的属性或所有样式/类标签。

这可能吗?

更新
如果 html 像 <div id="testDiv">cfwcvb</div> 怎么办?和 #testDiv 的 CSS 类是在外部样式表中定义的吗?

更新 2
很抱歉没有早点澄清这一点

如果这是我的 HTML

<div id="divId">
<span class="someClass">Some innerText</span>
</div>

样式在单独的样式表或头部样式中定义。

#divId {
clear: both;
padding: 3px;
border: 2px dotted #CCC;
font-size: 107%;
line-height: 130%;
width: 660px;
}
.someClass {
color: blue;
}

然后当我尝试获取 $("#divId").html() 的内部 html 时或调用任何其他自定义函数,我需要如下内容

<style>
#divId {
clear: both;
padding: 3px;
border: 2px dotted #CCC;
font-size: 107%;
line-height: 130%;
width: 660px;
}
.someClass {
color: blue;
}
</style>

<div id="divId">
<span class="someClass">Some innerText</span>
</div>

kirilloid 的回答更新 3
我在 this 的 Chrome 调试器工具的命令窗口中运行了以下代码页面本身,这就是我所看到的TypeError: Cannot read property 'rules' of undefined

function getElementChildrenAndStyles(selector) {
var html = $(selector).get(0).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[i].rules || sheets[i].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(CSSrule); break;
}
}
}
}
var style = rulesUsed.map(function(cssRule){
if ($.browser.msie) {
var cssText = cssRule.style.cssText.toLowerCase();
} else {
var cssText = cssRule.cssText;
}
// some beautifying of css
return cssText.replace(/(\{|;)\s+/g, "\$1\n ").replace(/\A\s+}/, "}");
// set indent for css here ^
}).join("\n");
return "<style>\n" + style + "\n</style>\n\n" + html;
};
getElementChildrenAndStyles(".post-text:first");

最佳答案

outerHTML (不确定,你需要它——以防万一)

限制: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, "\$1\n ").replace(/\A\s+}/, "}");
// set indent for css here ^
}).join("\n");
return "<style>\n" + style + "\n</style>\n\n" + html;
}

用法:

getElementChildrenAndStyles("#divId");

关于javascript - jQuery - 如何使用元素的 HTML 获取所有样式/css(在内部/外部文档中定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com