2011-01-19 6 views
2

난이 형식의 배열이 : 내가 alert(points[1]);를 사용할 때 ... 주위 몇 HTML 코드가 있기 때문에 내가 분명히 [object object]를 얻을배열 객체 내부의 텍스트를 검색하는 방법은 무엇입니까?

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

사람이 어떻게 내부의 텍스트를 검색하는 말해 줄 수 이 배열. "hi"또는 "hello"또는 "call"또는 "crow"로만 출력이 필요합니다.

감사합니다.

답변

1

적절한 구문, 자바 스크립트가 내용을보고하지 않는 points[1].text

HTML 코드가 있기 때문이 아닌 것이다. 푸시 된 각 요소에 대해 푸시 한 각 객체에 대해 자바 스크립트 객체 리터럴 { property : "value" } 의미를 사용하고 있습니다. 인스턴스화 된 속성을 점 표기법 object.property에 값을 지정하여 참조해야합니다. json을 사용하지 않으려면 push 문을 이와 같이 변경하십시오.

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 {}없이) 구문의 유형을 사용하는 경우, 당신은 단순히 당신이 원래 의도처럼 referance에 수있는 문자열 배열을해야합니다.

또한이 문자열 값에 무엇이 포함되어 있는지 신경 쓰지 않으며 다른 문자열과 마찬가지로 취급합니다.

0

alert(points[1].text)은 예를 들어 '<span style="font-weight: bold;font-size:25px;">hi</span>' 텍스트를 제공합니다. 그 후에 적절한 내용을 추출해야합니다.

관련 문제