繁体   English   中英

jQuery attr('href')不起作用

[英]jquery attr('href') not working

更新,问题已解决。 问题出在我试图更改HREF(实际上是将其变回)之后调用的函数。


使用jQuery,我试图根据URL中包含的哈希值更改一系列链接的HREF属性,但是该属性只是没有变化,而是被更改为空字符串。

有人可以在这里看到我在做什么错吗? 为什么不起作用?

http://fwy.pagodabox.com/categories/sculptures/#grid

有问题的链接是

所有展览装置物件印刷品雕塑

在二级导航中

function navHash($navlinks, hashtxt) {

        // loop through specified links
        $navlinks.each(function(){
            var $me = $j(this),
                myhref,
                index;

            // Does this link have an href… if not move on          
            if( typeof $me.attr('href') === "undefined" )
                return false;

            myhref = $me.attr('href');
        index = myhref.indexOf('#');

        // if my href doesn't have the specified hash text, add it, else remove it
        if(myhref.indexOf(hashtxt) === -1) {
            $me.attr("href", hashtxt);
        } else {
            $me.attr("href", myhref.substring(0, index));
        }   
    });
}




   $j(document).ready(function($){

        var $navlinks = $j('.sub-nav li:not(.views) a');

        if(window.location.hash == '#grid') {
            navHash($navlinks, '#grid');
            $('.views .ic-grid').click();
        }
// so on...

谢谢!!!

如果您在此处返回false ,则将停止循环。

if(typeof $me.attr('href') === "undefined")
   return false;

返回true继续循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM