简体   繁体   English

使用jQuery更改背景图片的值

[英]Changing value of background-image using jQuery

I'm trying to change the background image of a span using jQuery. 我正在尝试使用jQuery更改跨度的背景图像。 Basically I want to keep switching between 'arrow-down.png' and 'arrow-up.png' The logic is: 基本上,我想一直在“ arrow-down.png”和“ arrow-up.png”之间切换。逻辑是:

  1. I click on the #profile-li span 我点击#profile-li跨度
  2. script checks what background image the .arrow-img span has currently got 脚本检查.arrow-img范围当前具有哪些背景图像
  3. if it contains 'down' change it to 'up' 如果包含“向下”,则将其更改为“向上”
  4. else change 'up' to 'down' 否则将“上”更改为“下”

Up to point 2. it works fine - it goes to the 'if' statement, but when I get the alert box the image is still 'arrow-down.png'. 到第2点为止,它仍然可以正常工作-转到'if'语句,但是当我收到警告框时,图像仍为'arrow-down.png'。 I have no idea what I'm doing wrong here: 我不知道我在做什么错:

 $("#profile-li").click(function () {
        var bgValue = $("#profile-li").children(".li-title").children(".arrow-img").css('background-image').toString();
        if (/down/i.test(bgValue)) {
            (bgValue).replace('down', 'up');
            alert(bgValue);
        }

        else
            $("#profile-li").children(".li-title").children(".arrow-img").css('background-image').replace('up', 'down');
    });

Could someone please help me to find and fix my mistake? 有人可以帮我发现并纠正我的错误吗?

You might try this (untested) 您可以尝试一下(未经测试)

$("#profile-li").click(function (){
    if($(this).children(".li-title")
              .children(".arrow-img")
              .css('background-image') == 'url(default/images/image-down.png)'){
        $(this).children(".li-title")
               .children(".arrow-img")
               .css('background-image', 'url(default/images/image-up.png)')
    }
    else{
        $(this).children(".li-title")
               .children(".arrow-img")
               .css('background-image', 'url(/default/images/image-down.png)')
    }
});

image-down.jpg and image-up.jpg are whatever the names of the bg images are named. image-down.jpgimage-up.jpg都是背景图像的名称。

EDIT 编辑

To be clear, as per the comments below, you also need to include the right file path and CSS background-image syntax. 为了清楚起见,按照下面的注释,您还需要包括正确的文件路径和CSS背景图像语法。 Changed above. 上面改了。

make use of "$(this)" inside your function. 在函数中使用“ $(this)”。 it would also help if you posted your html 如果您发布了html也将有所帮助

toString() returns a string and not a jquery object so changing bgValue (the string...) has no effect on your page. toString()返回一个字符串而不是一个jquery对象,因此更改bgValue (字符串...)对您的页面无效。

I would simply add a class with the down-arrow, set the up-arrow as the default background and use jquery's toggleClass() method. 我只需要添加一个带有向下箭头的类,将向上箭头设置为默认背景,并使用jquery的toggleClass()方法。

Something like (I´d need to see the html for an exact solution): 类似的东西(我需要查看html以获得确切的解决方案):

$("#profile-li").click(function () {
  $(this).toggleClass("down_arrow");
}

You could always change it up so you have two classes in the CSS. 您可以随时对其进行更改,因此CSS中有两个类。 This lets you have the ability to change the look of the arrows in the future and not have to change anything in the script file. 这样一来,您便可以将来更改箭头的外观,而不必更改脚本文件中的任何内容。

.up {
    background-image: arrow-up.jpg;
}
.down {
    background-image: arrow-down.jpg;
}

Then in the JS you can just add or remove the class as you need it. 然后,在JS中,您可以根据需要添加或删除类。

$("#profile-li").click(function (){
    var arrow = $(this).children(".li-title").children(".arrow-img");
    if (arrow.hasClass("up")) {
           arrow.addClass("down").removeClass("up");
    } else {
           arrow.addClass("up").removeClass("down");
    }
});

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

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