在jQuery中,有没有类似于html()或text()的函数但是返回匹配组件的全部内容?
例如,如果匹配是
Hello world
,我需要返回
Hello world
不只是“你好世界”。
谢谢!
获取outerHTML没有内置函数,但您可以使用:
jQuery.fn.outerHTML = function(s) { return (s) ? this.before(s).remove() : jQuery("").append(this.eq(0).clone()).html(); }
然后在你的选择器中:
$('.class1').outerHTML()
将为您提供所需的内容。
function的来源
看看这个outerHTML插件 。
@Jose Basilio的回答很棒。 但是当使用IFrame时,Brian Grinstead在这个函数中发现了一个问题: http ://www.briangrinstead.com/blog/jquery-outerhtml-snippet
在这里,我将Jose的方式与Brian的IFrames解决方案一起设置了外部 HTML :
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(); } }
我使用.andSelf()成功: