Get selected element's outer HTML

前端 未结 30 2354
礼貌的吻别
礼貌的吻别 2020-11-21 04:50

I\'m trying to get the HTML of a selected object with jQuery. I am aware of the .html() function; the issue is that I need the HTML including the selected obje

30条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 05:19

    I agree with Arpan (Dec 13 '10 5:59).

    His way of doing it is actually a MUCH better way of doing it, as you dont use clone. The clone method is very time consuming, if you have child elements, and nobody else seemed to care that IE actually HAVE the outerHTML attribute (yes IE actually have SOME useful tricks up its sleeve).

    But I would probably create his script a bit different:

    $.fn.outerHTML = function() {
        var $t = $(this);
        if ($t[0].outerHTML !== undefined) {
            return $t[0].outerHTML;
        } else {
            var content = $t.wrap('
    ').parent().html(); $t.unwrap(); return content; } };

提交回复
热议问题