어떻게 부모에게 텍스트를 출력 할 수 있습니까 <div>
의 <span>
? 내가 도대체 뭘 잘못하고있는 겁니까? 스팬 및 .parent()
으로 작업하는 것보다 더 좋은 해결책이 있다면 알려주십시오.Jquery 부모의 범위
HTML :
<div>
<span></span> <!--the span where i want to output "troll" and "dwarf"-->
<a href="#" class="heroes" alt="troll">Icon</a>
<a href="#" class="heroes" alt="dwarf">Icon</a>
</div>
<div>
<span></span <!--the span where i want to output "witch"-->
><a href="#" class="heroes" alt="witch">Icon</a>
</div>
<div>
<span></span> <!--etc...-->
<a href="#" class="heroes" alt="barbarian">Icon</a>
</div>
<div>
<span></span>
<a href="#" class="heroes" alt="necromancer">Icon</a>
</div>
JS :
$('.heroes').hover(function() {
var hero = $(this).attr('alt');
$(this).parent('span').text(hero);
}, function() {
$(this).parent('span').text("");
});
감사
"참고 : alt는 앵커에 유효한 HTML 속성이 아닙니다." 그게 문제가 되나요? alt 대신 무엇을 제안하나요? – John
또한 "hero"에 대한 정의가이 함수에서 정의되지 않았습니다. "요소 영역을 벗어난 후 텍스트가 사라 지길 원합니다. 영웅이 필요하지 않은 이유입니다. :) – John
@ user6613. ... :) – gdoron