2011-09-01 57 views
3

嗨HREF的一部分,我必須在CMS幾個書籤:刪除使用JQuery

<li><a href="#online">Chat Online</a> </li> 
<li><a href="#termsOfService">Terms of Service</a> </li> 

但CMS是打破我的功能鏈接#前添加一些網址。 有沒有什麼辦法可以刪除#之前的所有鏈接#Jquery。 感謝您的幫助

+0

得到了解決....謝謝:) – Sonu

回答

0

這些元素有一個id?如果他們這樣做,你可以做這樣的事情,是XXX的每個元素

$(document).ready(function() { 
    var href = $('#XXX').attr('href'); 
    href = href.split('#')[1]; 
    $('#XXX').attr('href', href); 
}); 

的內部ID如果你想在你頁面這適用於每一個錨,那麼:

$(document).ready(function() { 
    $('a').each(function() { 
     var href = $(this).attr('href'); 
     href = href.split('#')[1]; 
     $(this).attr('href', href); 
    }); 
}); 

希望這幫助

0

試試這個

工作demo

$(function(){ 
    $("a").each(function(){ 
     if(this.href && (this.href.indexOf("#") != -1)){ 
      this.href = this.href.split("#")[1]; 
     } 
    }); 
}); 
3

您可以編寫一個正則表達式來刪除href屬性中的散列之前的所有內容。我建議做它在服務器端的,但如果你必須使用jQuery它可能看起來像this

$('li a').each(function() { 
    var href = $(this).attr('href').replace(/.*(#.*)$/, "$1"); 
    $(this).attr('href', href); 
});