【问题标题】:Jquery Getting a relative path for an image swap (not the rollover image)Jquery获取图像交换的相对路径(不是翻转图像)
【发布时间】:2011-01-14 03:54:54
【问题描述】:

当页面另一部分的链接翻转时,我正在尝试替换图像。我有一个硬编码版本,但我想得到它,这样当我上传到服务器时,我不需要进入并更改路径部分。

$(document).ready(function() {
$('.laundryLinks li a').hover(function() {
 $('.homeLaundryPict').attr('src', 'http://192.168.1.2:8888/karenMaezenMiller/wp-content/themes/karenMillerTheme/i/laundryHome_quote.jpg'); },
 function() {
 $('.homeLaundryPict').attr('src', 'http://192.168.1.2:8888/karenMaezenMiller/wp-content/themes/karenMillerTheme/i/laundryHome_noQuote.jpg'); });
});

PS。我也尝试过尝试替换一些源,但没有运气

$(this).src.replace("_quote","_noQuote");

【问题讨论】:

  • 您的文档看起来如何?
  • 你不能只使用相对链接吗? (相对于页面或站点/应用程序的根目录。)

标签: jquery image replace path


【解决方案1】:

您需要执行以下操作才能使其正常工作:

$(this)[0].src = $(this)[0].src.replace(/_quote/, '_noQuote');

希望对你有帮助!

编辑

$(function() {
    $('.laundryLinks li a').hover(function() {
        with ( $('.homeLaundryPict')[0] )
            src = src.replace(/_quote/, '_noQuote');
    }, function() {
        with ( $('.homeLaundryPict')[0] )
            src = src.replace(/_noQuote/, '_quote');
    });
});

给你...

【讨论】:

  • 谢谢 aefxx,我如何将 .homeLaundryPict 集成到其中?以下没有奏效。干杯。 $(document).ready(function() { $('.laundryLinks li a').hover(function() { $('.homeLaundryPict').src = $(this)[0].src.replace(/ _noQuote/, '_quote')); }, function() { $('.homeLaundryPict').src = $(this)[0].src.replace(/_quote/, '_noQuote'); ); }); });
  • 对不起,代码乱七八糟,看不到,让我把它放在代码标签中
【解决方案2】:

试试这个(假设只有第一个元素会交换):

var pic = $('.homeLaundryPict')[0];
var orig = pic.src;
$('.laundryLinks li a').hover(function() {
    pic.src = pic.src.replace(/_quote/,"_noQuote");
}, function() {
    pic.src = orig;
});

【讨论】:

  • 这看起来应该可以工作,但事实并非如此。不过谢谢!
【解决方案3】:

你想要做的替换应该是

$img = $('#img_selector');
$img.attr('src', $img.attr('src').replace("_quote","_noQuote") );

[更新]

您的代码(在评论中)有一些语法错误..(以下是更正的版本..

$(document).ready(function() { 
  $('.laundryLinks li a').hover(
    function() { 
      $img = $('.homeLaundryPict'); 
      $img.attr('src', $img.attr('src').replace("_noQuote","_quote") );
    }
    , 
    function() { 
      $img = $('.homeLaundryPict'); 
      $img.attr('src', $img.attr('src').replace("_quote","_noQuote") );
  } );
});​

【讨论】:

  • 我试过这个没有运气,语法错误? $(document).ready(function() { $('.laundryLinks li a').hover(function() { $img = $('.homeLaundryPict'); $img.attr('src', $img. attr('src').replace("_noQuote","_quote") );, function() { $img = $('.homeLaundryPict'); $img.attr('src', $img.attr(' src').replace("_quote","_noQuote") );); });
  • replace(regexp, String) 是正确的签名而不是 replace(String, String)。请看我的回答。
  • @aefxx,根据w3schools.com/jsref/jsref_replace.asp,它可以是正则表达式或字符串..
  • @Eric C,你用 ) 而不是 } 关闭内部函数,看看你的代码。我将把它作为更新添加到我的答案中。
  • 非常感谢,这是一个设计师尝试编码的结果!唉,似乎仍然缺少一些东西......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 2011-08-25
相关资源
最近更新 更多