简体   繁体   中英

if the link contains the word “photo” in a link

http://link.com/photo/ photo /user?=19746912

See the bold part. How can i look into this link and confirm it has the word "photo" in its second level as there are two photos in the link i could not use this code:

$('a[href*=photo]).attr('href');

As you can see from the above code , it looks for the link that has photo in it then get the href out of it. the problem is what if i have two links containing photo like this:

<a href="http://link.com/photo/photo/user?=19746912"></a>

<a href="http://link.com/photo/album/user?=19746912"></a>

You can see that the first level of the both links has photo word , which is not good if im using the above code. so how can i look at the links second level , which link1 is photo , link2 is album , and get the one with the second level "photo"(first link)'s href.

I am using the latest jQuery.

Thanks.

Can you not do this:

$('a[href*="photo/photo"]').attr("href")

Or if you want to do something more complicated you can test the href using a regex:

$('a').filter(function() {
    return /\/photo\/[^\/]+$/.test(this.href);
});

(The regex I've included for demonstration purposes matches "/photo/" followed by one or more non-slash characters at the end of the string, so it will match your first anchor but not the second.)

Demo: http://jsfiddle.net/XAnAq/1/

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