Get selected element's outer HTML

前端 未结 30 2466
礼貌的吻别
礼貌的吻别 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've used Volomike's solution updated by Jessica. Just added a check to see if the element exists, and made it return blank in case it doesn't.

    jQuery.fn.outerHTML = function() {
    return $(this).length > 0 ? $(this).clone().wrap('
    ').parent().html() : ''; };

    Of course, use it like:

    $('table#buttons').outerHTML();
    

提交回复
热议问题