how to print entire HTML element in JavaScript?

I want to print the entire element including tag name, attribute name/value pairs and innerHTML. How can I do it in JavaScript (jQuery)?

for example:

var elArr = document.getElementsByTagName('link');
alert(elArr[0].printEntireElement());

//expected output might be 
<link href="/css/common.css" rel="stylesheet" type="text/css">`

Note that for link element outerHTML is not defined!


使用一个像这样的外部HTMLHTML插件,或者这个插件。


如果你不能做outerHtml,请克隆它,创建一个新的div,将克隆放入div中,并获取容器的innerHTML。


document.getHTML= function(who){
    var txt, ax, el= document.createElement("div");
    el.appendChild(who.cloneNode(false));
    txt= el.innerHTML;      
    ax= txt.indexOf('>')+1;
    txt= txt.substring(0, ax)+who.innerHTML+ txt.substring(ax);
    el= null;
    return txt.replace(/>/g,'>n');
}
链接地址: http://www.djcxy.com/p/83360.html

上一篇: Click事件不适用于动态添加的按钮

下一篇: 如何在JavaScript中打印整个HTML元素?