gpt4 book ai didi

html - 如何检索数组对象中的文本?

转载 作者:行者123 更新时间:2023-11-28 02:15:34 25 4
gpt4 key购买 nike

我有一个这种格式的数组:

points.push(
{text: '<span style="font-weight: bold;font-size:25px;">hi</span>'},
{text: '<span style="font-weight: bold;font-size:25px;">hello</span>'},
{text: '<span style="font-weight: bold;font-size:25px;">call</span>'},
{text: '<span style="font-weight: bold;font-size:25px;">crow</span>'});

当我使用 alert(points[1]); 我得到 [object object] 显然是因为它周围有一些 html 代码...

谁能告诉我如何检索此数组中的文本。我只需要输出“hi”或“hello”或“call”或“c​​row”

谢谢。

最佳答案

正确的语法是points[1].text

不是因为有html代码,javascript没有看内容。对于每个推送的元素,您正在使用 javascript 对象文字 { property : "value"} 表示您推送的每个对象。您必须引用您实例化的属性,方法是在点符号 object.property 中为其分配一个值。如果您不想使用 json,请像这样更改您的推送语句。

points.push(
'<span style="font-weight: bold;font-size:25px;">hi</span>',
'<span style="font-weight: bold;font-size:25px;">hello</span>',
'<span style="font-weight: bold;font-size:25px;">call</span>',
'<span style="font-weight: bold;font-size:25px;">crow</span>');

如果您使用这种类型的语法(没有 json {}),您将只拥有一个字符串数组,您可以像最初预期的那样引用它们。

此外,请理解它不关心这些字符串值中包含的内容,它会像对待任何其他字符串一样对待它们。

关于html - 如何检索数组对象中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4732069/

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