getComputedStyle of a clone element which is not in the dom

流过昼夜 提交于 2020-01-31 18:26:21

问题


I have a html page like;

<div id="cloneDiv">
    <h1>
        <a href="">ASUS Vivotab Smart Black Office 2013 H&S ME400C-C2-BK 10.1-Inch 64GB Tablet
            (Black)</a>
    </h1>
    <div id="fordBackgroundImage" style="display: inline-block; background-position: center center; width: 1200px;
        height: 565px; background-image: url('http://www.ford.com/ngbs-services/resources/ford/edge/2013/npcolorizer/edg13_ingotsilver.jpg');
        background-repeat: no-repeat; background-attachment: scroll; position: relative;">
        <div style="position: absolute; color: white; font-weight: bold; top: 50px; left: 100px; font-size: 50px;">
            Try new ford jeep!
        </div>
    </div>
    <p>
        <img alt="" src="http://ecx.images-amazon.com/images/I/81UsKLK%2B6LL._SX342_.jpg"
            class="border thumb0 selected" style="cursor: pointer;">
    </p>
    <p>
        <img border="0" src="http://g-ecx.images-amazon.com/images/G/01/kindle/otter/dp/o1_slate_sm._V358729399_.jpg"
            style="width: 320px; height: 282px;">
    </p>
 </div>

and I have a javascript function to convert element to computed style element;

function MakeCssInline(elementParam) {
var domE = $(elementParam).get()[0];
var cssText = window.getComputedStyle(domE).cssText;
$(elementParam).attr("style", cssText);
// children
var items = domE.getElementsByTagName("*");
for (var i = 0; i < items.length; i++) {
    var domE2 = items[i];
    var cssText2 = window.getComputedStyle(domE2).cssText;
    $(items[i]).attr("style", cssText2);
}
 };

If I call function on original element it works; but it changes original element html

MakeCssInline($("#cloneDiv"));

But When I try to call this function on cloned element it does not work;

var cloneElement = $("#cloneDiv").clone();
MakeCssInline(cloneElement);

it does not apply styles to element because I did not append in the body element, since I do not want to mess with original element, is there any way to get computed style clone of an element with javascript?


回答1:


I wrote a very basic function that follows Felix Kling's suggestion;

function MakeCssInline(elementParam) {
    var cloned = $(elementParam).clone();
    $(cloned).find("script").remove();
    $(cloned).find("link").remove();
    $(cloned).find("iframe").remove();
    //
    var domE = $(elementParam).get()[0];
    var domCloned = $(cloned).get()[0];
    //
    var cssText = window.getComputedStyle(domE).cssText;
    $(cloned).attr("style", cssText);
    // children
    var items = domE.getElementsByTagName("*");
    var itemsCloned = domCloned.getElementsByTagName("*");
    for (var i = 0; i < items.length; i++) {
        var domE2 = items[i];
        var cssText2 = window.getComputedStyle(domE2).cssText;
        $(itemsCloned[i]).attr("style", cssText2);
    }
    return domCloned;
};



回答2:


Try to append the elemnt to the body, hidden, rip the info you need, then remove it from DOM.



来源:https://stackoverflow.com/questions/18706243/getcomputedstyle-of-a-clone-element-which-is-not-in-the-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!