0

我从我的页面进行了 ajax 调用,然后作为响应,我也得到了一些像这样的 html

<parent id="1"><child></child></parent>

我想要的是从 Response 对象中获取内部 HTML,不包括<parent> 我该怎么做?document.getElementbyID不能在变量上使用。

4

3 回答 3

9

您可以为变量内容创建一个 jQuery 包装器,然后使用.html()提取内部 html

var data = '<parent id="1"><child></child></parent>'
var x = $(data).html()
于 2013-10-15T15:15:18.177 回答
0

如果你喜欢纯 JS -> http://jsfiddle.net/eztZm/

//get
var get_html = document.getElementById("parent").innerHTML;
console.log(get_html);
//set 
document.getElementById("parent").innerHTML = "new html";

https://developer.mozilla.org/en-US/docs/Web/API/Element.innerHTML

于 2013-10-15T15:25:58.373 回答
0

使用的html代码:

<parent id="1"><child>candy</child></parent>

第一种方法:

var parent = document.getElementById("1");
var child_text = parent.firstChild.innerHTML;

使长话短说:

document.getElementById("1").firstChild.innerHTML

将提供“糖果”(没有 jQuery):)

于 2013-10-15T15:30:33.490 回答