简体   繁体   English

检查链接(不是URL)是否包含一些文本(.jpg,.gif,.png)

[英]Check if link (not URL) contain some text (.jpg, .gif, .png)

Just to give an Idea what i'm trying to do here's an example code: 只是为了给出一个想法,我在这里尝试做的是一个示例代码:

$(function(){
  if ($('.maybe > div > a.link:contains(".JPG, .jpg, .gif, .GIF")').length) {
    alert('hello');
});

I want to check if the content of some links are containing the dot and the letters of all image extensions, like 我想检查一些链接的内容是否包含点和所有图像扩展名的字母,如

<div class="maybe">
 <div>
   <a class="link" href="someURL">thisIsAnImage.jpg</a>
   <a class="link" href="someURL">thisIs**NOT**AnImage.pdf</a>
 </div>
</div>
 <div class="maybe">
 <div>
   <a class="link" href="someURL">thisIs**NOT**AnImage.zip</a>
   <a class="link" href="someURL">thisIsAnotherImage.png</a>
 </div>
</div>

The div's and links are generated dynamically by php, so there's no way to know how many links and div's there will be once the page is generated. div和链接是由php动态生成的,所以一旦页面生成,就无法知道有多少链接和div。

How to write the code in a properply way? 如何以适当的方式编写代码?

Thanks a lot for helping me to resolve the problem. 非常感谢帮助我解决问题。

Here's my first instinct: 这是我的第一直觉:

$('.maybe .link').each(function () {
    if ($(this).text().toLowerCase().match(/\.(jpg|png|gif)/g)) {
        console.log("yay I did it");
    }
});

Use toLowerCase() on the link text so you don't have to check both lower and upper case. 在链接文本上使用toLowerCase(),这样您就不必检查大小写。 Then use String.match(regex) with a regex group to match all the file extensions. 然后将String.match(regex)与regex组一起使用以匹配所有文件扩展名。

Hope this helps! 希望这可以帮助!

Edit: here's an example in jsfiddle. 编辑:这是jsfiddle中的一个例子。 Open your javascript console to see the output of the console.log statement. 打开javascript控制台以查看console.log语句的输出。 http://jsfiddle.net/9Q5yu/1/ http://jsfiddle.net/9Q5yu/1/

I'd suggest: 我建议:

// selects all the 'a' elements, filters that collection:
var imgLinks = $('a').filter(function(){
    // keeps *only* those element with an href that ends in one of the
    // file-types (this is naive, however, see notes):
    return ['png','gif','jpg'].indexOf(this.href.split('.').pop()) > -1;
});
// I have no idea what you were doing, trying to do, wanting to do or why,
// but please don't use 'alert()', it's a horrible UI:
if (imgLinks.length) {
    console.log('hello');
}

The above is a relatively simple, and naive, check; 以上是一个相对简单,天真的检查; in that it simply splits the href on the . 因为它只是将href拆分为. characters and then tests the last element from the array (returned by split() ) is equal to one of the elements of the array. 字符然后测试数组中的最后一个元素(由split()返回)等于数组的一个元素。 This will fail for any image that has a query string, for example, such as http://example.com/image2.png?postValue=1234 对于具有查询字符串的任何图像,这将失败,例如http://example.com/image2.png?postValue=1234

Given the clarification in the comments, I'd amend the above to: 鉴于评论中的澄清,我将上述内容修改为:

var fileTypes = ['png','gif','jpg','gif'],
    imgLinks = $('a').filter(function(){

    return (new RegExp(fileTypes.join('|') + '$', 'gi')).test($(this).text());
});

References: 参考文献:

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

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