简体   繁体   English

如果链接中的链接中包含单词“ photo”

[英]if the link contains the word “photo” in a link

http://link.com/photo/ photo /user?=19746912 http://link.com/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. 从上面的代码中可以看到,它将查找其中包含照片的链接,然后从中获取href。 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. 您会看到两个链接的第一级都带有photo word,如果我使用上述代码,效果不好。 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. 所以我怎么看第二级的链接,其中link1是photo,link2是相册,并获得具有第二级“ photo”(第一个链接)的href的链接。

I am using the latest jQuery. 我正在使用最新的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: 或者,如果您想做更复杂的事情,可以使用正则表达式测试href:

$('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.) (出于演示目的,我包含的正则表达式在字符串末尾匹配“ / photo /”,后跟一个或多个非斜杠字符,因此它将与您的第一个锚点匹配,但与第二个锚点不匹配。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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