2012-02-01 9 views

답변

0

첫번째 방법 :

$(function(){ 
$("#a_theme").click(
    function(){ 
     $("#a_theme").attr("href", "new url"); 
    } 
); 
}); 

<a href="#" id="a_theme">Click</a> 

두번째 방법 :

function activate_theme(){ 
    $("#a_theme").attr("href", "new url"); 
} 


<a href="#" id="a_theme" onClick="activate_theme()">Click</a> 
0

당신은 잘못하고있다 :

function activate_theme(){ 
    $("#a_theme")).attr("href", "new url"); 
} 

그것은 오류를 표시합니다

<a href="exampleurl" id="a_theme" onClick="return activate_theme();">Click</a> 

이것은 jQuery 코드는 다음과 같습니다

는 HTML 코드입니다 activate_theme()onClick 처리기에서 액세스 할 수없는 범위에 정의되어 있습니다. 또한 구문 오류가 있습니다 (jQuery 호출에서 double )).

그냥이 당신의 코드를 대체 :

<a href="exampleurl" id="a_theme">Click</a> 

와 자바 스크립트를 :

DOM이 준비, 지정된 링크 href를 대체하는 즉시 실행됩니다
$(function(){ 
    $("#a_theme").attr("href", "new url"); 
}); 

.

데모 여기서 일하는 참조 : http://jsfiddle.net/tadeck/mHtHc/

0
try with this also: 

$('#a_theme').click(function(){ 
$(this).attr('href', 'new_url'); 
}); 

하지만 당신은 같은 이름을 가진 두 개 이상의 ID를 가지고있는 경우 :

$('#a_theme').each(function(i, e){ 
    $(this).click(function(){ 
    //same url on all '#a_theme' on click 
    $(this).attr('href', 'new_url'); 
    }); 
}); 
관련 문제