【问题标题】:Relative image source path returns null using jQuery使用 jQuery 的相对图像源路径返回 null
【发布时间】:2012-04-30 17:41:35
【问题描述】:

我有一个问题需要帮助。

我正在尝试使用 jQuery 创建鼠标悬停动画,以便当用户将鼠标悬停在图像上时,它会显示图像的突出显示版本。我知道我可以用 CSS 做到这一点,但问题是需要管理内容。所以我想写一个函数来匹配图像文件名的一部分,并为文件名的其余部分使用通配符。

这里是 HTML: 这是始终显示的图像,除非用户将鼠标悬停在图像上。

<img class="imgPath" src="<?php echo PATH_SITE. 'uploads/folder/bgr_img.jpg' ?>" alt="First Image" />

当用户将鼠标悬停在图像上时,我想更改 src:

<img class="imgPath" src="<?php echo PATH_SITE. 'uploads/folder/hvr_image.jpg' ?>" alt="First Image" />

鼠标悬停时,我希望 src 恢复到之前的状态“bgr_image.jpg”

这是我目前使用的 jQuery:

$(function() {
    $(".imgPath")
        .mouseover(function() { 
            var src = $(this).attr("src").match("http://domain.com/path/to/img/hvr_image.jpg");
            $(this).attr("src", src);
        })
        .mouseout(function() {
            var src = $(this).attr("src").replace("http://domain.com/path/to/img/hvr_image.jpg", "http://domain.com/path/to/img/bgr_image.jpg");
            $(this).attr("src", src);
        });
});

如果我现在将鼠标悬停在图像上,它会将 src 更改为“null”。我尝试使用不包含域的路径名,但它返回相同的值。

我们将不胜感激。

【问题讨论】:

    标签: javascript jquery html image relative-path


    【解决方案1】:

    “匹配”函数返回布尔值,而不是字符串(在鼠标悬停函数中)

    【讨论】:

    • 否,“match”返回数组中的匹配项,如果不匹配则返回 null。
    【解决方案2】:

    为什么要匹配你的 src 或替换它?只需更改它:

    $(function() { 
        $(".imgPath").hover(
            function() { $(this).attr("src", "http://domain.com/path/to/img/hvr_image.jpg"); },
            function() { $(this).attr("src", "http://domain.com/path/to/img/bgr_image.jpg"); }
        ); 
    }); 
    

    编辑:

    match() 返回匹配的数组:使用 [0] 访问您的 src

    $(function() { 
        $(".imgPath") 
            .mouseover(function() {  
                var src = $(this).attr("src").match("http://domain.com/path/to/img/hvr_image.jpg"); 
                $(this).attr("src", src[0]); 
            }) 
            .mouseout(function() { 
                var src = $(this).attr("src").replace("http://domain.com/path/to/img/hvr_image.jpg", "http://domain.com/path/to/img/bgr_image.jpg"); 
                $(this).attr("src", src); 
            }); 
    });
    

    EDIT2:

    <img class="imgPath" onmouseover="changeImgSrc(this, '<?php echo PATH_SITE. 'uploads/folder/hvr_image.jpg' ) ?>'" onmouseout="changeImgSrc(this, '<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg' ) ?>'" src="<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg' ?>" alt="FirstImage" />
    
    <script>
        function changeImgSrc(img, imgsrc) {
            $(img).attr('src', imgsrc);
        }
    </script>
    

    或者:

    <img class="imgPath" onmouseover="this.src = '<?php echo PATH_SITE. 'uploads/folder/hvr_image.jpg' ) ?>'" onmouseout="this.src = '<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg' ) ?>'" src="<?php echo PATH_SITE. 'uploads/folder/bgr_image.jpg' ?>" alt="FirstImage" />
    

    【讨论】:

    • 因为我想使用通配符,以便我可以应用于网站该部分中的所有图像。此方法只能应用于 1 个实例,我如何将其应用于 5 个具有 5 个不同名称的图像。把函数写五遍?看起来很草率。
    • 它仍然返回 null。如果我在匹配函数中使用相同的图像,则会加载。它不会显示任何错误,因为存在匹配并且 src 不为空。但是,如果尝试将其与悬停状态的图像匹配,它会告诉我 src 为空。
    • 我在我的帖子 (EDIT2) 中添加了一个新的解决方案。也许这可以帮助你。
    • 它使 html 变得杂乱无章,但它必须这样做,感谢您抽出时间 Andreas。它工作正常。 :)
    【解决方案3】:

    此任务无需使用 ma​​tch()replace() 函数

    请按以下步骤操作。

    $(function() {
        $(".imgPath")
            .mouseover(function() { 
    
                $(this).attr("src", "http://domain.com/path/to/img/bgr_image.jpg");
    
            })
            .mouseout(function() {
                $(this).attr("src", "http://domain.com/path/to/img/hvr_image.jpg");
    
            });
    });
    

    已编辑:

    假设您是否对多张图片有这种情况:

    <div class="imgPath"><img src="1.jpg" /> </div>
    <div class="imgPath"><img src="2.jpg"  /></div>
    <div class="imgPath"><img src="3.jpg" /> </div>
    <div class="imgPath"><img src="4.jpg"  /></div>
    

    所以我们可以用下面的代码来管理这个:

    $('.imgPath img').hover(
        function(){
            $('.imgPath img').each(
                function(){
                    $(this).attr("src", "http://domain.com/path/to/img/bgr_image.jpg");
                });
        },
        function(){
            $('.imgPath img').each(
                function(){
                  $(this).attr("src", "http://domain.com/path/to/img/hvr_image.jpg");
                });
        }
    
    );
    

    我认为这会对你有更多帮助。

    谢谢。

    【讨论】:

    • 是的,这行得通,就像前面的答案一样。但是我想将此应用于多个图像。使用这种方法,我只能将其应用于 1 张图片。
    • 请检查我编辑的答案部分。谢谢
    猜你喜欢
    • 2011-09-05
    • 2010-11-29
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2019-11-24
    • 2017-06-19
    • 1970-01-01
    • 2010-09-05
    相关资源
    最近更新 更多