html原生从json获取数据,html到json获取div数据(html to json get div data)

html到json获取div数据(html to json get div data)

我想将html页面div标签转换为json数据,并将该div添加到另一个html page.get中,使用id进行div。

假设有page.html,其中有div标签,有id page1 page2等等。将它们转换为json数据并根据它们的id获取div标签,然后将这些div标签附加到page2.html div标签。 怎么做。

这是page2.html

this is page1

this is page1

this is page1

this is page1

有div标签

我尝试使用js获取div但是从另一个页面这是page1.html并且想要访问page2.html的div

function getdata()

{

$.get('page2.html', null, function(text){

alert($(text).find('#page1'));

});

var json = JSON.stringify(element);

alert(json);

}

我尝试了这个,但它不起作用。

I want to convert html page div tag to json data and append that div in another html page.get that div that using id.

suppose there is page.html in which div tags are there, having id page1 page2 and so on.Convert those to json data and get the div tags according to their id, then append those div tags to page2.html div tag. how to do that.

this is page2.html

this is page1

this is page1

this is page1

this is page1

having div tags

I tried getting div using js but from another page this is page1.html and want to access div of page2.html

function getdata()

{

$.get('page2.html', null, function(text){

alert($(text).find('#page1'));

});

var json = JSON.stringify(element);

alert(json);

}

I tried this but its not working.

原文:https://stackoverflow.com/questions/41930833

更新时间:2020-03-02 20:18

最满意答案

你需要在json字符串中编写div对象,将div转换为具有所有必需属性和值的json对象。 例如,请参见下面的代码段。

function append(){

var element = document.getElementById("page1");

var jsonObject = {};

jsonObject.id = element.id;

jsonObject.innerHTML = element.innerHTML;

var jsonString = JSON.stringify(jsonObject); // this is json for your div.

/// for append div and get div object back from json.

var elementProto = JSON.parse(jsonString);

var element = document.createElement("DIV");

element.innerHTML = elementProto.innerHTML;

element.id = elementProto.id;

// append to container (in your case its page 1 or 2)

document.getElementById("container").append(element);

}

this div will appended

append div

you need to write div object in json string , convert div into json object with all required attributes and values. see below snippet for example.

function append(){

var element = document.getElementById("page1");

var jsonObject = {};

jsonObject.id = element.id;

jsonObject.innerHTML = element.innerHTML;

var jsonString = JSON.stringify(jsonObject); // this is json for your div.

/// for append div and get div object back from json.

var elementProto = JSON.parse(jsonString);

var element = document.createElement("DIV");

element.innerHTML = elementProto.innerHTML;

element.id = elementProto.id;

// append to container (in your case its page 1 or 2)

document.getElementById("container").append(element);

}

this div will appended

append div

相关问答

既然你说JSON正在回归,并且假设问题中的对象结构是正确的,那么我会发现一些事情: 1)我不明白为什么这行var tr = data.report在代码中。 您将tr设置为已获取的数据,然后在循环中使用jquery对象覆盖它。 2)您无法从JSON中获取数据的原因是data是对象的根。 您尝试写入表的data位于顶级data对象的report属性中。 您可以将您的javascript更新为: for (var i = 0; i < data.report.length; i++) {

var

...

您的函数的结果是promise,而不是字符串内容。 因此,您需要使用已解析的值进行分配: function sidebarContent() {

return fetch('*url*', {

headers: {

'Accept': 'application/json, text/plain, */*'

}

})

.then(response => {

return response.json().t

...

当你这样做 document.querySelector(`.club-info`).appendChild($club);

document.querySelector(`.club-info`).appendChild($origin);

document.querySelector(`.club-info`).appendChild($championships);

,它每次都会.club-info第一个.club-info元素。 它不知道你想要使用也存储在$div的元素。 由于$div包

...

您所要做的就是编写几个循环来迭代数组。 这是使用.forEach()的示例解决方案。 var obj = {

"myMenu":[

{

"id":"1",

"name":"name 01",

"image":"img_url",

"other":[

{

"image":"img_url",

...

我建议采用以下方法: // using 'let' to declare variables, rather than 'var'

// if you require compatibility with ES5 then use

// 'var' instead:

let data = {

// content of JSON removed for brevity

"articles": [...]

},

// creating an element:

ima

...

$.each(data.data, function (key, value) {

$("#enqfield_show").html("

" + value + "");

$("#enqfield_show").append("

" +key +" : "+ value + "");

});

$.each(data.data, function (key

...

你需要在json字符串中编写div对象,将div转换为具有所有必需属性和值的json对象。 例如,请参见下面的代码段。 function append(){

var element = document.getElementById("page1");

var jsonObject = {};

jsonObject.id = element.id;

jsonObject.innerHTML = element.innerHTML;

var jsonString

...

你不需要使用$.getJSON来获取已经在html div上的数据。 如果您的数据如下所示

[{"name":"LOG_DATE","data":["2015-09-08","2015-09-09"]},{"name":"a","data":[30.5‌​,87.5]},{"name":"b","data":[55.5,82.4]},{"name":"c","data":[82,57.46]},{"name":"d‌​","data":[82.4,8

...

首先,如果你使用phonegap你需要在javascript中向你的php服务器发一个ajax请求来获取你的用户列表,那么你可以注入你的html 请参阅: http : //tournasdimitrios1.wordpress.com/2011/11/04/how-to-generate-json-with-php-from-mysql-and-parse-it-with-jquery/ first if you are using phonegap you need to make an aj

...

缺少关于DOM树中你真正想要放置数据的地方的任何信息...... $('#someElement').append($('

', {text: sound[i].title }));

Lacking any information about where in the DOM tree you'd actually like to put the data... $('#someElement').append($('

', {text: sound[i].title }));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值