将jQuery对象打印为HTML [英] Print out jQuery object as HTML

查看:96
本文介绍了将jQuery对象打印为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种很好的方式将jQuery对象打印为纯HTML?

Is there a good way of printing out a jQuery object as pure HTML?

例如:

<img src="example.jpg">

<script>
var img = $('img').eq(0);
console.log(img.toString());
</script>

toString()不能这样工作.我需要HTML等效字符串,即

toString() doesn't work this way. I need the HTML equivalent string , i.e:

<img src="example.jpg">

推荐答案

如果您需要以HTML格式打印对象,请使用此扩展名 outerHTML .

If you need to print the object in HTML format, use this extension outerHTML or this outerHTML.

更新

更新链接,并包含第二个链接的代码:

Update the link and include the code for second link:

$.fn.outerHTML = function(){

    // IE, Chrome & Safari will comply with the non-standard outerHTML, all others (FF) will have a fall-back for cloning
    return (!this.length) ? this : (this[0].outerHTML || (
      function(el){
          var div = document.createElement('div');
          div.appendChild(el.cloneNode(true));
          var contents = div.innerHTML;
          div = null;
          return contents;
    })(this[0]));

}

这篇关于将jQuery对象打印为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆