开发者

How to retrieve the text inside the objects of array?

开发者_Go百科i have an array of this format:

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>'});

when i use alert(points[1]); i get [object object] obviously because there are some html code around it...

can anyone tell me how to retrieve the text inside this array. i need the output as only "hi" or "hello" or "call" or "crow"

Thank you.


the proper syntax would be points[1].text

It is not because there is html code, javascript isn't looking at the contents. For each pushed element you are using a javascript object literal { property : "value" } meaning for each object you pushed. You would have to reference the property you instantiated, by assigning it a value, in dot notation object.property. if you want to not use json, change your push statement like so.

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>');

If you use this type of syntax, (without json {}) you will simply have an array of strings that you could referance like you originally intended.

Also, understand that it does not care what is contained in these string values, it treats them just like any other string.


alert(points[1].text) will give you the text: '<span style="font-weight: bold;font-size:25px;">hi</span>' for example. You will have to extract the proper content after that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜