[英]How do I convert a JSON serialized object into a JS object, so I can access its properties?
[英]How do I remove access JS object properties with double quotation
我从URL提取数据。 数据看起来像下面的代码。 如何访问图像源和内容? (请注意,当我console.log data.description时,它的控制台未定义)。
data:{
title: "title of the data"
description:"<div class="feed-description"><p style="text-align: center;"><img alt="" src="http://....jpg" style="width: 600px; height: 350px;" /></p><p><government's financial crisis?</p><p>Working out us realling good for your body(<em>depass</em>).</p><p>Finance Ministry insider disclosed that the annual <em>depass</em> at the State House cost.</p></div>"
}
假设获得的响应为常数:
const data = {
title: "title of the data",
description:`<div class="feed-description"><p style="text-align: center;"><img alt="" src="http://....jpg" style="width: 600px; height: 350px;" /></p><p><government's financial crisis?</p><p>Working out us realling good for your body(<em>depass</em>).</p><p>Finance Ministry insider disclosed that the annual <em>depass</em> at the State House cost.</p></div>`
}
如果您想访问描述HTML字符串的内容,则需要解析此字符串,以创建一个虚拟DOM元素并将该字符串添加到其中。 然后,您可以像处理任何DOM元素一样操作它。
例:
const el = document.createElement('div');
el.innerHTML = data.description;
// Getting the image source
const img = el.getElementsByTagName('img')[0]; // matching the first position
const source = img.src;
// Getting the div content
const divContent = el.getElementsByTagName('div')[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.