2012-02-06 53 views
0

請原諒我的無知,但我一直在尋找這一段時間,我無法弄清楚如何更改以下的href:如何使用jQuery更改href屬性onmouseover?

<a id="p2749244"class="nohover"onfocus="this.blur()"name="index"rel="history"onmouseout="this.className='nohover';"onmouseover="this.className='hover';"href="address"> 

做什麼樣的選擇我需要用做這個?提前致謝。

+0

你應該使用'setAttribute' - 儘管我會按照Daniels的建議去使用jQuery來完成整個代碼。 – 2012-02-06 13:53:56

回答

6
onmouseover="this.href = 'urlHere';" 

或者使用jQuery,您可以使用類選擇。

$('.nohover').hover(function(){ 
    this.href = "urlHere"; 
}); 

而不是內聯事件處理使用jQuery來更好地管理它,試試這個。

$('.nohover').hover(function(){ 
    $(this) 
    .addClass('hover') 
    .removeClass('nohover') 
    .attr('href', 'urlHere'); 
}, function(){ 
    $(this) 
    .addClass('nohover') 
    .removeClass('hover'); 
}).focus(function(){ 
    $(this).blur(); 
}); 
1

爲什麼不用jQuery來做這件事?我認爲它可能比老式的DOM方法更可靠。

$(document).ready(function() { $('#p2749244').attr('href', 'new address'); }); 
+0

不夠公平,但我看不出有什麼做的所有的問題(即使IE瀏覽器,在其所有的輝煌crappiness,支持'getElementById'):) – karim79 2012-02-06 13:52:02

+0

一) OP已經在使用jQuery和b)它會工作 – 2012-02-06 13:52:57

1
onmouseover="this.href = 'blahblah'" 
+0

這不是jQuery的問題 – 2012-02-06 14:55:40

+0

只有區別是,即使沒有包含jQuery,這也會起作用。哦,它更短。 – karim79 2012-02-06 15:02:27

1
$('a[name="index"]').hover(
function(){$(this).attr(href,'new_href_here')}, 
function(){//code for mouse out here} 
)