동일한 클래스 이름을 갖고 앵커 태그 인 HTML 문서의 모든 요소를 찾아서 텍스트를 가져 오려고합니다.배열의 요소에 대한 JQuery .text()
HTML :
<div class="links" id="one">
<span class="info">useless links</span>
<a class="anchorTag" href="www.sample.com">Click me!</a>
<a class="anchorTag" href="www.sample.com">Click me again!</a>
<a class="anchorTag" href="www.sample.com">Click me once again!</a>
<a class="anchorTag" href="www.sample.com">Click me one last time!</a>
</div>
<div class="links" id="two">
<span class="info">secret links</span>
<a class="anchorTag" href="www.sample.com">Click me!</a>
<a class="anchorTag" href="www.sample.com">Click me again!</a>
<a class="anchorTag" href="www.sample.com">Click me once again!</a>
<a class="anchorTag" href="www.sample.com">Click me one last time!</a>
</div>
코드 :
$('.links').each(function(){
if($(this).find("span.info").text() == "secret link"){
var allLinks = $(this).find("a.anchorTag");
console.log(allLinks[0].text());
}
});
오류 : 나는 VAR의 길이를 allLinks를 인쇄 할 경우
console.log(allLinks[0].text()) is not a function
, 그것은 올바른 크기를 반환하지만, i에 액세스 할 때 배열의 특정 요소의 텍스트를 인쇄하지 않습니다. 색인과 함께? 왜 이렇게이다?
아아아 ... 나는 그것이 세트라는 것을 알지 못했지만이 주제에 대한 나의 전체적인 질문은 분명합니다. 그래서 집합을 배열로 변환하는 것이 가능할 것입니다 만, anchorTag에 직접 주소를 지정하고 텍스트를 파싱하는 것으로 가정합니다. – Bajro
jquery의 .toArray()를 사용하여 true 배열로 변경할 수 있습니다. 메서드를 사용하지만 jquery로 다시 래핑하지 않으면 더 이상 jquery 연산을 수행 할 수 없습니다. – Cameron637