开发者

If else and href ends in problem JQuery

开发者 https://www.devze.com 2023-01-31 20:52 出处:网络
I\'m struggling with the following code. The first if statement is doing what I want, then I need to make sure I don\'t add a parameter to an href if it ends in .pdf ... the second if statement is not

I'm struggling with the following code. The first if statement is doing what I want, then I need to make sure I don't add a parameter to an href if it ends in .pdf ... the second if statement is not working. I'm sure it is something simple because I know very 开发者_如何学编程little! Please help. Thanks!

var string2 = "x";

if (document.domain.indexOf(string2)!== -1)   {
}

else {

$("#breadcrumb a").each(function(){                         
    this.search = 'lnkid=bc';    
});

if ($("#linklist a[href$='.pdf']") > -1 )   {
}
else {  
$("#linklist a").each(function(){                         
    this.search = 'lnkid=snav';    
});

}


Why not just:

$("#linklist a").not("[href$='.pdf']").each(function () {
     ... 
} );


It should be written as:

$("#linklist a").each(function() {
    if($(this).attr("href").indexOf(".pdf") == -1) {
        this.search = "lnkid=snav";
    }
}    

You want to iterate through each a tag only once. Your current code is going through each a tag, and basically doing nothing, and then going through each a tag again, and always adding the lnkid into the search attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消