2014-01-30 108 views
0

我想禁用鏈接時,我一個鏈接上點擊,這裏是我的代碼:如何禁用鏈接時,其點擊?

<a href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a> 
<a href="?cmd=14" style="color:#00F; margin-left:15px; text-decoration:underline">Past 14 Days</a> 
<a href="?cmd=30" style="color:#00F; margin-left:15px; text-decoration:underline">Past 30 Days</a> 
<a href="?cmd=custom" style="color:#00F; margin-left:15px; text-decoration:underline">Set A Custom Date Range</a> 

我希望當我點擊過去7天鏈接此鏈接激活,儘快那麼如果我點擊禁用或療法鏈接在過去14天的鏈接中,過去7天的鏈接已啓用,過去14天的鏈接已停用。請問我這樣做?

+0

我是唯一一個在這裏誰認爲你想實現頁面加載效果? –

+0

沒有人回答幫助我 –

回答

3
$('a').on('click',function(){ 
    $('a').removeAttr('disabled'); 
    $(this).attr('disabled',true); 
}) 

.disable 
{ 
pointer-events: none; 
cursor: default; 
} 

$('a').on('click',function(){ 
    $('a').removeClass('disable'); 
    $(this).addClass('disable'); 
}) 
+0

我認爲OP希望在頁面加載時達到效果。 –

+0

當我使用你的代碼 –

+0

它給出錯誤TypeError:$(...)爲空 –

0

試試這個:

$('a').on("click", function (e) { 
    e.preventDefault(); 
}); 
0

由於我的名聲不允許我評論,我會發布在這裏作爲答案。這是爲了改進Karthick Kumar Ganesh的回答。

根據您使用的jq版本,您應該使用.prop()而不是.attr()來設置屬性/屬性。

另外我會在你的鏈接上使用一個類,如果你不只是尋址錨或添加獲取參數,應該禁用,以便在點擊後不會禁用所有鏈接。

$("a.disabable").on('click',function(){ 
    $(this).prop('disabled',true); 
}) 

最後它非常有意義添加一個類似於你的鏈接target="_blank"什麼的,讓你的鏈接在新標籤中打開。否則,在點擊鏈接後禁用鏈接是沒有意義的。

一個例子鏈接看起來像這樣

<a href="wher/you/want/to/go" class="disabable" target="blank" style="[...]">Foo</a> 
0

這是迄今爲止解決方案,整個頁面加載的作品所描述的OP(我假設在錨點擊更改文件位置,因此重新加載頁面)。

HTML(加class屬性):

<a class="cmd-7" href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a> 
<a class="cmd-14" href="?cmd=14" style="color:#00F; margin-left:15px; text-decoration:underline">Past 14 Days</a> 
<a class="cmd-30" href="?cmd=30" style="color:#00F; margin-left:15px; text-decoration:underline">Past 30 Days</a> 
<a class="cmd-custom" href="?cmd=custom" style="color:#00F; margin-left:15px; text-decoration:underline">Set A Custom Date Range</a> 

的JavaScript(使用this answer代碼):我認爲它會工作

$(function() { 
    // Get the cmd query parameter 
    var cmd = getParameterByName('cmd'); 
    if(cmd) { 
    // Disable the link 
    $('a.cmd-' + cmd).click(function(event) { 
     event.preventDefault(); 
    }) 
    // Add a class to allow styling 
    .addClass('disabled'); 
    } 
}); 
+0

當我使用你的代碼時,它給出了錯誤ReferenceError:getParameterByName未定義 –

+0

如何刪除這個 –

+0

@Arif我寫了「使用該代碼[此答案](http://stackoverflow.com/questions/901115/how-can -i-得到的查詢字符串值入的JavaScript/901144#901144)」。所以你應該包含在參考答案中聲明的函數。 –

0
.disabled { 
text-decoration:none; 
color:black; 
} 


<a class="links" href="##"> Link1 </a></br> 
<a class="links" href="##"> Link2 </a></br> 
<a class="links" href="##"> link3 </a></br> 


$(function(){ 
    $(".links").click(function(){ 
    if($(this).hasClass("disabled")){ 
     return false; 
    } 
    else{ 
     $(".links").removeClass("disabled"); 
     $(this).addClass("disabled"); 
    } 
}); 
})