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

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

For example, if the match is

Hello world
, I need to return

Hello world
4条回答
  •  梦谈多话
    2020-12-30 10:31

    @Jose Basilio's answer is great. But Brian Grinstead found a problem in this function when using IFrames: http://www.briangrinstead.com/blog/jquery-outerhtml-snippet

    Here I put together Jose's way for also setting the outerHTML with Brian's solution for IFrames:

    jQuery.fn.outerHTML = function(s) {
        if (s) {
            return this.before(s).remove();
        } else {
            var doc = this[0] ? this[0].ownerDocument : document;
            return jQuery('
    ', doc).append(this.eq(0).clone()).html(); } }

提交回复
热议问题