我理解以下 jquery 代码捕获了上述 <div>
中的所有 html 文本内容类:
$("div.content").html();
不过,我还想捕获格式,例如表格边框及其背景颜色。我该怎么做,因为 html()
只捕获文本及其格式?
希望有人指教。谢谢。
最佳答案
您可以使用此函数获取所有样式、内联样式、计算样式等。
随心所欲地编辑,它有效。检查这个 jsfiddle:http://jsfiddle.net/65adr/40/
$.fn.copyCSS = function (source) {
var dom = $(source).get(0);
var dest = {};
var style, prop;
if (window.getComputedStyle) {
var camelize = function (a, b) {
return b.toUpperCase();
};
if (style = window.getComputedStyle(dom, null)) {
var camel, val;
if (style.length) {
for (var i = 0, l = style.length; i < l; i++) {
prop = style[i];
camel = prop.replace(/\-([a-z])/, camelize);
val = style.getPropertyValue(prop);
dest[camel] = val;
}
} else {
for (prop in style) {
camel = prop.replace(/\-([a-z])/, camelize);
val = style.getPropertyValue(prop) || style[prop];
dest[camel] = val;
}
}
return this.css(dest);
}
}
if (style = dom.currentStyle) {
for (prop in style) {
dest[prop] = style[prop];
}
return this.css(dest);
}
if (style = dom.style) {
for (prop in style) {
if (typeof style[prop] != 'function') {
dest[prop] = style[prop];
}
}
}
return this.css(dest);
};
关于javascript - 捕获 div 类的内容,包括 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20281143/