Getting InnerHtml from a javascript Variable

孤人 提交于 2019-12-12 04:35:41

问题


I make an ajax call from my page , and then in response I also get some html like this

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

what I want is to get Inner HTML from the Response object excluding <parent> How can I do that? Cant use document.getElementbyID on a variable.


回答1:


you can create a jQuery wrapper for the variable content and then extract the inner html using .html()

var data = '<parent id="1"><child></child></parent>'
var x = $(data).html()



回答2:


Pure JS if you like -> 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




回答3:


html code used:

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

first approach:

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

to make a long story short:

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

will deliver "candy" (without jQuery) :)



来源:https://stackoverflow.com/questions/19384868/getting-innerhtml-from-a-javascript-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!