2013-11-01 42 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