Jquery 如果IMG SRC中存在URL元素

Jquery 如果IMG SRC中存在URL元素,jquery,image,url,src,javascript,Jquery,Image,Url,Src,Javascript,jQuery代码如何编写成这样的状态:“如果img src中存在‘page.php’,那么就不要在该img上运行‘example function’。如果img src中不存在‘page.php’,那么就在该图像上运行‘example function’。” 该页面将有多个图像,并且并非所有图像都在图像src中有“page.php” 以下是我迄今为止尝试但未成功的代码: <script type="text/javascript"> if (jQuery('img').attr('s

jQuery代码如何编写成这样的状态:“如果img src中存在‘page.php’,那么就不要在该img上运行‘example function’。如果img src中不存在‘page.php’,那么就在该图像上运行‘example function’。”

该页面将有多个图像,并且并非所有图像都在图像src中有“page.php”

以下是我迄今为止尝试但未成功的代码:

<script type="text/javascript">
if (jQuery('img').attr('src').indexOf('page.php') >= 0) {
    example();
}
</script>

我对代码进行了注释,因此它应该是不言自明的:

jQuery(document).ready(function () {
  var contentWidth = $('#content').width();
  //find all images that do not have phpThumb.php in their src attr
  $('#content img:not([src*="phpThumb.php"])')
    //set their SRC attribute based on their current SRC attribute
    .attr('src', function (index, attr) { return '/util/phpThumb/phpThumb.php?src=' + attr.replace(window.location.protocol + "//" + window.location.host, '') + '&w=' + contentWidth });

  $(window).resize(function () {
    var contentWidth = $('#content').width();
    //find all thumbnails (those containing the string phpThumb.php iin their src attribute)
    var thumbs = $('#content img[src*="phpThumb.php"]')
                  //hide them (why?)
                  .hide()    
                  //update their SRC attribute, use regular expression to find the old width, and replace it with the new onee
                  .css('src', function (index, attr) { return attr.replace(/w=\d*$/, 'w=' + contentWidth); })
                  //show them again (why?)
                  .show()
  });
});

我对代码进行了注释,因此它应该是不言自明的:

jQuery(document).ready(function () {
  var contentWidth = $('#content').width();
  //find all images that do not have phpThumb.php in their src attr
  $('#content img:not([src*="phpThumb.php"])')
    //set their SRC attribute based on their current SRC attribute
    .attr('src', function (index, attr) { return '/util/phpThumb/phpThumb.php?src=' + attr.replace(window.location.protocol + "//" + window.location.host, '') + '&w=' + contentWidth });

  $(window).resize(function () {
    var contentWidth = $('#content').width();
    //find all thumbnails (those containing the string phpThumb.php iin their src attribute)
    var thumbs = $('#content img[src*="phpThumb.php"]')
                  //hide them (why?)
                  .hide()    
                  //update their SRC attribute, use regular expression to find the old width, and replace it with the new onee
                  .css('src', function (index, attr) { return attr.replace(/w=\d*$/, 'w=' + contentWidth); })
                  //show them again (why?)
                  .show()
  });
});

谢谢你,吉登!不幸的是,我的代码肯定有不同的问题。修复并不像我想象的那么简单——不过,从什么时候开始?!:)我将粘贴完整的代码,并修改我的问题。也许这样会更有意义。Gidon-你知道如何根据我原来问题的更新让代码工作吗?谢谢你迄今为止的帮助!哇,吉顿,太棒了!它起作用了!只有一个小问题。当窗口调整大小时,图像实际上不会调整大小。如果我刷新页面,图像将调整大小。除了那件小事,你的编程技巧真是太棒了!你知道,这可能没问题,因为大多数人不会在笔记本电脑和台式机以外的设备上调整窗口大小,这些设备通常有足够好的互联网连接来处理较大/未调整大小的图像。非常感谢你!!!现在,我正在玩这段代码,我们是否可以更进一步,让脚本在其他选择器上运行,而不是在内容上运行,例如:.1、.2、.3、.4等等。?我试着用逗号分隔新的选择器,但没有用。只有当只有一个选择器(例如“.three”)时,脚本才会运行。有什么想法吗?谢谢你,吉顿!不幸的是,我的代码肯定有不同的问题。修复并不像我想象的那么简单——不过,从什么时候开始?!:)我将粘贴完整的代码,并修改我的问题。也许这样会更有意义。Gidon-你知道如何根据我原来问题的更新让代码工作吗?谢谢你迄今为止的帮助!哇,吉顿,太棒了!它起作用了!只有一个小问题。当窗口调整大小时,图像实际上不会调整大小。如果我刷新页面,图像将调整大小。除了那件小事,你的编程技巧真是太棒了!你知道,这可能没问题,因为大多数人不会在笔记本电脑和台式机以外的设备上调整窗口大小,这些设备通常有足够好的互联网连接来处理较大/未调整大小的图像。非常感谢你!!!现在,我正在玩这段代码,我们是否可以更进一步,让脚本在其他选择器上运行,而不是在内容上运行,例如:.1、.2、.3、.4等等。?我试着用逗号分隔新的选择器,但没有用。只有当只有一个选择器(例如“.three”)时,脚本才会运行。有什么想法吗?