2010-01-08 6 views
2

을 HREF로 리디렉션,하지만 난이 작성하는 깨끗한 방법을 알아낼 수 없습니다링크 클릭 기능 내가이 질문을 너무 바보 같은 기분

을 heres HTML

<li> 
<a class='link_parent' href='#'>Test</a> 
</li> 

나는 .link_parent와 a의 HREF ...

정도 ... 리디렉션 부모 LI의 클릭 기능을 원하는

$('a.link_parent').each(function() { 
    $(this).parent().click(function() { 
    // Need to access $(this).attr('href') of the above object 
    // but can't since $(this) is now the <li> object 
    // help! 
    }); 
}); 

답변

2
$('a.link_parent').each(function() { 
    var link = this; 
    $(this).parent().click(function() { 
     link.attr('href'); 
    }); 
}); 
1
$("li").click(function(){ 
    $("a.link_parent", this).trigger("click"); 
}); 

범위에 대한 간단한 설명.

$("a.link_parent").each(function(i,o){ 
    // 'o' will always be a reference to this anchor 
    $(this).parent().click(function(e){ 
    // 'e.currentTarget' will be a reference to this parent 
    alert(o + " " + e.currentTarget); 
    }); 
}); 
0

당신이 그것을 기록의 정확한 JQuery와 방법을 알고 round.Dont 다른 방법으로 수행 할 수 있습니다 this 항목에 대한 액세스를 얻을 수있는 유일한 방법은 아니다.

"li"요소에 click 이벤트를 추가하고 첫 번째 자식 인 "a : 요소"를 가져 와서 해당 요소를 가져옵니다.