1

我有以下代码返回函数中每个链接的 url:

var nav_link;
$('#nav_what-we-do').find('.top-dropdown').find('a').each(function(index, elem) {
    console.log("Descendant: " + elem.tagName + " " + elem.href);
    the_link = $(this);
    nav_link = elem.href.split('#');
    if (nav_link[0] === '/what-we-do/') {
        the_link.attr('href','#'+nav_link[1]);
        the_link.click(function() {
            var tab_id = $(this).attr('href');
            selectTab(tab_id);
            return false;
        });
    }
});

控制台打印如下链接:

Descendant: A http://localhost/testsite/what-we-do/#solutions
Descendant: A http://localhost/testsite/what-we-do/#features
Descendant: A http://localhost/testsite/what-we-do/features/test
Descendant: A http://localhost/testsite/what-we-do/solutions/test2

现在我只想要那些包含哈希 (#) 的链接并使用此哈希拆分它们。然后我想检查 array(0) 是否包含字符“/what-we-do/”。我试图像这样拆分链接:

nav_link = elem.href.split('#');

IF部分不起作用。

有人可以告诉我我做错了什么。

编辑

根据TJ的建议

$('#nav_what-we-do').find('.top-dropdown').find('a').each(function(index, elem) {
    //console.log("Descendant: " + elem.tagName + " " + elem.href);
    var the_link = elem.href,
        hashIndex = the_link.split("#");
    if(hashIndex.length == 2) {
        console.log(hashIndex);
        console.log("Descendant: " + elem.tagName + " " + elem.href);
        console.log('First part is: ' + hashIndex[0].indexOf("/what_we_do/"));
    }
    if (hashIndex.length == 2 && hashIndex[0].indexOf("/what_we_do/") !== -1) {
        the_link.attr('href', "#" + hashIndex[1]);
        the_link.attr('href', the_link.substring(hashIndex));
        the_link.click(function() {
            var tab_id = $(this).attr('href');
            selectTab(tab_id);
            return false;
        });
    }
});

如果我在控制台中打印,我会得到:

["http://localhost/testsite/what-we-do/", "solutions"] site_javascript.js:133
Descendant: A http://localhost/testsite/what-we-do/#solutions site_javascript.js:134
First part is: -1 site_javascript.js:135

["http://localhost/testsite/what-we-do/", "features"] site_javascript.js:133
Descendant: A http://localhost/testsite/what-we-do/#features site_javascript.js:134
First part is: -1 

第一部分应该是值hashIndex[0].indexOf("/what_we_do/")

由于hashIndex有字符串/what-we-do/ 会发生什么?

4

2 回答 2

3

我想你可能想要:

var nav_link;
$('#nav_what-we-do').find('.top-dropdown').find('a').each(function(index, elem) {
    console.log("Descendant: " + elem.tagName + " " + elem.href);
    var the_link = elem.href,
        hashIndex = the_link.indexOf("#");
    if (hashIndex !== -1 && the_link.indexOf("/what-we-do/") !== -1) {
        the_link.attr('href', the_link.substring(hashIndex));
        the_link.click(function() {
            var tab_id = $(this).attr('href');
            selectTab(tab_id);
            return false;
        });
    }
});

(请注意,我声明了the_link; 如果它已经在迭代器函数之外的某个地方声明,您可以删除它,但它似乎应该是本地的。)

或者(从技术上讲/what-we-do/,如果在哈希之后,上述内容将匹配) :

var nav_link;
$('#nav_what-we-do').find('.top-dropdown').find('a').each(function(index, elem) {
    console.log("Descendant: " + elem.tagName + " " + elem.href);
    var the_link = elem.href,
        split = the_link.split("#");
    if (split.length === 2 && split[0].indexOf("/what-we-do/") !== -1) {
        the_link.attr('href', "#" + split[1]);
        the_link.click(function() {
            var tab_id = $(this).attr('href');
            selectTab(tab_id);
            return false;
        });
    }
});

在那里,我对其进行了限制,因此如果在第一个散列之后有散列,则它不匹配。

于 2012-07-09T16:36:17.307 回答
0

这是失败的,因为您正在检查整个绝对路径 (http://.....) 与它的标记 (/what-we-do/)。

有几种方法可以解决这个问题 - 最简单的方法可能是检查您要查找的令牌是否在 URL 中(而不是像目前那样完全等于它)。

if (nav_link.indexOf('#') != -1 && nav_link[0].indexOf('/what-we-do/') != -1) {
    ...
于 2012-07-09T16:38:03.093 回答