2

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

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”

谢谢你。

4

2 回答 2

1

正确的语法是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 {}),您将只拥有一个字符串数组,您可以像最初预期的那样引用这些字符串。

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

于 2011-01-19T05:38:11.060 回答
0

alert(points[1].text)会给你文字:'<span style="font-weight: bold;font-size:25px;">hi</span>'例如。之后,您必须提取正确的内容。

于 2011-01-19T05:37:48.260 回答