简体   繁体   中英

apply param to links inside div from the url value using jquery

So I have a URL like:

www.site.com/nice.html?menu=1

and I'm trying to find all links inside a div called.. ".page" and add "&menu=1" to them based on the menu parameter inside the url string.

I hope this makes sense. I'm going to create a jsfiddle.

Here is my JS so far.

$(".page a").each( function(){
//get page param with getmenu    
var getmenu = "";
var attr = $(this).attr("href"); 
attr+"&menu="+getmenu; 
}); 

http://jsfiddle.net/kWAxe/

How about:

$(".page a").each( function(){
    //get page param with getmenu    
    var getmenu = location.href.split("?")[1];
    var attr = $(this).attr("href"); 
    $(this).attr("href", attr + "&" + getmenu);
}); 

Check this. You have to reset the variable back to the href attribute. Alos you need to correct the name of your variable.

var gemenu = 0;

$(".page a").each( function(){
    //get page param with getmenu    
    gemenu += 1;
    var attr = $(this).attr("href"); 
    attr += "&menu="+gemenu;
    $(this).attr("href", attr);
}); 

http://jsfiddle.net/kWAxe/2/

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