简体   繁体   中英

How to add href using jQuery

I'm making adrop down menu using the following jquery:

$(document).ready(function(){
    // executed after the page has finished loading
   $('#navigationMenu li .normalMenu').each(function(
      $(this).before($(this).clone().removeClass().addClass('hoverMenu'));
   });
   $('#navigationMenu li').hover(function(){    
      $(this).find('.hoverMenu').stop().animate({marginTop:'0px'},200)
   },
   function(){
     $(this).find('.hoverMenu').stop().animate({marginTop:'-25px'},200);
   });
});

I need help adding the href from ONLY the clones created.

You could remove href when you are cloning them using .removeAttr()

  $(this).before($(this).clone().removeClass().addClass('hoverMenu').removeAttr('href'));

to add href you can use .attr()

 $(this).before($(this).clone().removeClass().addClass('hoverMenu').attr('href','#'));

To add the href attribute use attr :

$(this).before(
   $(this)
    .clone()
    .addClass('hoverMenu')
    .attr('href', 'http://google.com')
);

Reference:

.attr( attributeName, value )


Description : Set one or more attributes for the set of matched elements.

  • attributeName

    Type: String

    The name of the attribute to set .

  • value

    Type: String or Number

    A value to set for the attribute.

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