繁体   English   中英

如果IMG SRC中存在URL元素

[英]If URL Element Exists in IMG SRC

怎样写成这样的jQuery代码说明:“如果img src中存在'page.php',则不要对该img运行'example function'。如果img src中不存在'page.php',则在该图像上运行“示例功能”。”?

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

到目前为止,这是我尝试过的未成功的代码:

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

编辑#1:

以下是我的全部脚本。 基本上,我不想在其src URL中已经包含“ phpThumb.php”的图像上运行此脚本。

var ssWidth = 0;
var ssSrc;
var ssImg;
var ssc;
jQuery(document).ready(function(){
    ssc = jQuery('#content');
    ssImg = ssc.children('img');
    ssSrc = ssImg.attr('src').replace(window.location.protocol + "//" + window.location.host,'');
    genImage();
    jQuery(window).resize(function(){
        if(ssc.width()!=ssWidth){
            genImage();
        }
    });
});
function genImage(){
    ssImg.hide();
    ssWidth = ssc.width();
    ssImg.attr('src','/util/phpThumb/phpThumb.php?src='+ssSrc+'&w='+ssc.width());
    ssImg.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()
  });
});

暂无
暂无

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

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