简体   繁体   中英

Change html content of DOM element and pass it on as an object

I have my html content as below :

<div id="divParent">
    <div id="divChild">
        <table>
            <tr>
                <td>
                    <a href="sometext"></a>
                </td>
            </tr>
            <tr>
                <td>
                    <a href="sometext"></a>
                </td>
            </tr>
        </table>
    </div>
</div>

I need to get html content of divParent div and pass it on as an html for printing purpose.
This I can achieve fairly simple way by $("#divParent").html() I also need to change the anchor tag text. Something like this for example document.getElementsByTagName("a").attr('href', '')
But I don't want to change the actual anchor element on webpage. I just want to pass the divParent html with changed anchor tag attribute. Please help

You can create a .clone() element, which can be manipulate and passed for printing.

Create a deep copy of the set of matched elements.

var cloneElement = $("#divParent").clone();
cloneElement.find('a').attr('href', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM