gpt4 book ai didi

javascript - 如何删除带有双引号的访问JS对象属性

转载 作者:行者123 更新时间:2023-12-03 01:07:12 25 4
gpt4 key购买 nike

我从 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]

关于javascript - 如何删除带有双引号的访问JS对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52341151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com