In jQuery, are there any function that similar to html() or text() but return the whole content of matched component?

前端 未结 4 1247
南旧
南旧 2020-12-30 09:43

For example, if the match is

Hello world
, I need to return

Hello world
4条回答
  •  忘掉有多难
    2020-12-30 10:27

    There's no built-in function for getting the outerHTML, but you can use this:

    jQuery.fn.outerHTML = function(s) {
    return (s)
      ? this.before(s).remove()
      : jQuery("

    ").append(this.eq(0).clone()).html(); }

    Then in your selector:
    $('.class1').outerHTML() will give you what you are looking for.

    Source of function

提交回复
热议问题