請求

2015-01-02 44 views
-4

我當前的按鈕的代碼是這樣的。請求

<a href="google.com" onClick="correct();"class="button big scrolly"4.6 Inches</a> 

我必須要改變HREF = 「google.com」 這個

<a href="javascript:void(0)" onclick="window.open(decodeURIComponent(window.location.href.substr(window.location.href.lastIndexOf('aref=')+5)), '_self');"> </a> 

什麼是做到這一點的最佳方式,而不會影響我的onClick =正確的是(); &按鈕類?

謝謝!

+1

你到底要問我們嗎?爲什麼你必須將'href'改爲別的? 「糾正()」是做什麼的?通常,最好的做法是不使用'onclick ='屬性。 – JLRishe

+1

我沒有看到這裏的jQuery + – Rafael

回答

0

如果您正在使用jQuery,爲什麼不這樣做:

<a id="correct" href="#" class="button big scrolly">4.6 Inches</a> 

而且

$("#correct").click(function(){ 
    correct(); 
    var uri = window.location.href.lastIndexOf('aref=')+5; 
    uri = window.location.href.substr(uri); 
    uri = decodeURIComponent(uri); 
    window.open(uri, '_self'); 
}); 
1

那麼你可以通過多種方法:

<a href="javascript:void(0)" onclick="correct();window.open(decodeURIComponent(window.location.href.substr(window.location.href.lastIndexOf('aref=')+5)), '_self');"> </a> 

或者,你可以正確的方法中使用window.open()

function correct(){ 
//your code for correct... 
window.open(...) 
} 

,只是使用correct()中的onclick。

+0

1 ......此外,創建一個'window.open'代碼,可以縮短這個了一下第二功能。 – rfornal

+0

或者你可以在'correct()'函數中加入'window.open'。這更像是寫內聯JavaScript然後傳遞方法。 –

0

您可以使用下面的代碼。一個元素可以同時具有onclick和onmousedown屬性。

<a href="javascript:void(0)" onClick="correct();"class="button big scrolly" onmousedown="window.open(decodeURIComponent( window.location.href.substr(window.location.href.lastIndexOf('aref=')+5)), '_self');"> </a> 
+1

@reneshu你不能有兩個onclick屬性... –

+0

謝謝。我的意思是onclick和onmousedown,因爲我已經寫在上面。我改變了它,但非常感謝你讓我知道 – raneshu

0

你可以這樣做:

<a href="google.com" onClick="linker();" class="button big scrolly">4.6 Inches</a> 

function linker() { 
    correct(); 
    var url = window.location.href.lastIndexOf('aref=')+5; 
    url = window.location.href.substr(url); 
    url = decodeURIComponent(url); 
    window.open(url, '_self'); 
}