简体   繁体   中英

jquery attr('href') not working

Update, problem solved. The issue was with a function that I was calling after trying to change the HREF, which was essentially changing it back.


Using jQuery, I am trying to change the HREF attribute of a series of links based on the hash contained in the URL, but the attribute simply is not changing, it's being changed to an empty string.

Can anyone see what I'm doing wrong here? Why isn't it working?

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

The links in question are

All Exhibitions Installations Objects Prints Sculptures

In the secondary nav

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...

Thank you!!!

If you return false here you will stop looping.

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

Return true to continue looping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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