在JQuery中获取Node的原始HTML [英] Get Raw HTML of Node in JQuery

查看:74
本文介绍了在JQuery中获取Node的原始HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 $(#parent)。html()来获取 #parent ,但是我怎么得到父本身的html?

I have used $("#parent").html() to get the inner html of #parent, but how do I get the html of the parent itself?

用例是,我抓取一个像这样的输入节点:

The use case is, I grab an input node like this:

var field = $('input');

我希望能够得到该节点的原始html( <输入类型='文字'> )与 field.html()类似,但返回空白。这可能吗?

I would like to be able to get the raw html of that node (<input type='text'>) with something like field.html(), but that returns empty. Is this possible?

推荐答案

或者您可以创建添加一个JQuery函数,如下所示:

Or you could create add an JQuery function like this:

jQuery.fn.outerHTML = function(s) {
  return (s)
  ? this.before(s).remove()
  : jQuery("<p>").append(this.eq(0).clone()).html();
}

所以你可以这样做:

$('input').outerHTML();

$('input').outerHTML("new html");

感谢 http://yelotofu.com/2008/08/jquery-outerhtml/

这篇关于在JQuery中获取Node的原始HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆