javascript - 捕获 div 类的内容,包括 CSS 样式

标签 javascript jquery html

我理解以下 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/

相关文章:

php - 抓取时的字体或 Unicode 问题

html - css中标签的分隔线

javascript - Image Fades Into Another,然后函数加载新网页

javascript - 考虑到上传过程是异步的,如何在将文件上传到 firestore 后获取文件的元数据作为函数的返回值?

javascript - 是否可以使用 javascript 从文件字段中获取 $_FILES ["inputID"] ["tmp_name"]?

jquery - 没有 jQuery Mobile 的 ListView

javascript - stub 使用 Sinon 执行回调的方法

javascript - jQuery "Chosen"过滤事件?

javascript - meteor 货币兑换

Javascript - 在写下文本区域之前更改输入字母