2013-11-01 43 views
0

我有下面這段代碼,指的是通常只有像「#programs」一樣的頁面鏈接的href。但是,我現在需要一些鏈接來引用其他頁面,如「/index.html#programs」。所以我的問題是,如何編輯下面的代碼中的「href」,以便只是數字符號和後面的所有符號?謝謝。刪除字符串href

var lastId, topMenu = $("#main-menu"), 
topMenuHeight = topMenu.outerHeight() + 500; 
menuItems = topMenu.find('a'); 

    scrollItems = menuItems.map(function() { 

     content = $(this).attr("href"); 

     if(content){ 
      var checkURL = content.match(/^#([^\/]+)$/i); 

      if(checkURL){ 

       var item = $($(this).attr("href")); 
       if (item.length) return item 

      } 
     } 
    }); 
+0

什麼是這樣做的目的是什麼? – charlietfl

回答

1

ü可以給這樣的新路徑:

var path = "ur_new_location"; 
$("#link1").attr('href',path); 
0
var item = "/index.html#programs"; 
var afterHash = item.substr(item.indexOf("#")) 

//這裏的竅門是的indexOf和SUBSTR方法

var lastId, topMenu = $("#main-menu"), 
topMenuHeight = topMenu.outerHeight() + 500; 
menuItems = topMenu.find('a'); 

    scrollItems = menuItems.map(function() { 

     content = $(this).attr("href"); 

     if(content){ 
      var checkURL = content.match(/^#([^\/]+)$/i); 

      if(checkURL){ 

       var item = $($(this).attr("href")); 
       item = item.substr(item.indexOf("#")) 
       if (item.length) return item 

      } 
     } 
    }); 
0

如果是我,我會忘記替換所有文件名並使用錨標記的hash property以獲得您想要的內容

替換你有過濾器:

.filter(function(){ 
    return this.hash==='#' +id; 
}); 

Simple demo