Get selected element's outer HTML

前端 未结 30 2347
礼貌的吻别
礼貌的吻别 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:01

    Short and sweet.

    [].reduce($('.x'), function(i,v) {return i+v.outerHTML}, '')
    

    or event more sweet with help of arrow functions

    [].reduce.call($('.x'), (i,v) => i+v.outerHTML, '')
    

    or without jQuery at all

    [].reduce.call(document.querySelectorAll('.x'), (i,v) => i+v.outerHTML, '')
    

    or if you don't like this approach, check that

    $('.x').get().reduce((i,v) => i+v.outerHTML, '')
    

提交回复
热议问题