检查img的src是否包含jQuery的某些文本

jef*_*lte 1 html javascript jquery

我希望使用jQuery contains来遍历页面上的每个图像,并检查src标记是否包含某个http值.如何使用jQuery或javacript对src属性中的文本进行检查.我概述了我在下面尝试的内容:

$('img').each(function(i){
  var img = $(this),
      imgSrc = img.attr('src'),
      siteURL = "http://url.com";

  if(!imgSrc.contains(siteURL)){
     imgSrc = siteURL + imgSrc;
  }
});                         
Run Code Online (Sandbox Code Playgroud)

我有一种感觉正则表达可能是走的路,只是不知道如何肯定.

Nic*_*tti 6

我会(使用indexOf):

$('img').each(function(i){
      var imgSrc = this.src;
      var siteURL = "http://url.com";

  if(imgSrc.indexOf(siteURL) === -1){
     imgSrc = siteURL + imgSrc;
  }
}); 
Run Code Online (Sandbox Code Playgroud)


Tej*_*ejs 5

为什么不简单地创建一个选择器来做到这一点呢?

 $('img[src*="http://url.com"]')
Run Code Online (Sandbox Code Playgroud)

这应该只选择 src 标记中包含该文本的元素,而无需编写自定义逻辑。