jQuery从图像src属性中删除文本

时间:2013-01-20 01:50:45

标签: jquery

使用jQuery,我想在悬停时更改src属性。例如,我有以下HTML:

<img src="convert.php?image=images/example.jpg" alt="#" />

在悬停时,我想删除“convert.php?”字符串,以便HTML为:

<img src="image=images/example.jpg" alt="#" />

一定很简单吗?

3 个答案:

答案 0 :(得分:2)

回答了自己的问题。找到这个片段。

$('.item img').each(function(e){
    var src = $(this).attr('src');
    $(this).hover(function(){
      $(this).attr('src', src.replace('convert.php?image=', ''));
    }, function(){
     $(this).attr('src', src);
    });
  });

答案 1 :(得分:0)

可以使用attr(function(){}

以下内容还会恢复mouseleave上的图片

$(".item img").hover(function () {
      var convert='convert.php?image=';
     $(this).attr('src',function(idx, attr){
          return attr.indexOf(convert) <0 ? convert+attr : attr.replace(convert,'');
     });
});

当只有一个函数作为参数传递给悬停时,它将同时针对mouseentermouseleave个事件运行

答案 2 :(得分:-1)

  $("img").hover(
      function () {
        var src = $(this).attr('src');
        jQuery.data(this, 'old_source', src);
        srcNew = src.replace("convert.php?", "");
        $(this).attr('src', srcNew);
        $(this)., srcNew);
      },
      function () {
        var src = $(this).data('old_source');
        $(this).attr('src', src);
      }
    );
相关问题