Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 背景图像赢得';不变_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 背景图像赢得';不变

Javascript 背景图像赢得';不变,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想在setInvterval中更改div的背景图像,但不知道如何更改。代码如下: $('.thumbnail').on({ 'mouseenter': function(){ var thumbs = $(this).attr('data-num-thumbs'); var thumb_url = $(this).css('background-image'); console.log(thumb_url)

我想在setInvterval中更改div的背景图像,但不知道如何更改。代码如下:

$('.thumbnail').on({
        'mouseenter': function(){
            var thumbs = $(this).attr('data-num-thumbs');
            var thumb_url = $(this).css('background-image');
            console.log(thumb_url);
            var i = 1;
            setInterval(function(){
                var new_thumb_url = thumb_url.replace(/\d{1,2}\.jpg/gi, i + '.jpg');
                console.log(new_thumb_url);
                $(this).css('background-image', new_thumb_url);
                i++;
                if(i > 10) i = 1;
            }, 3000);
        });
});
这就是
div
的样子:

<div data-num-thumbs="17" class="thumbnail" style="background: url('http://site/img/11.jpg') no-repeat;"></div>

它看起来像是
$(此)
内部的
setInterval()
正在映射到其他对象,请尝试以下操作

$('.thumbnail').on({
    'mouseenter': function(){
        $this=$(this);  //store $(this) in a local var
        var thumbs = $(this).attr('data-num-thumbs');
        var thumb_url = $(this).css('background-image');
        console.log(thumb_url);
        var i = 1;
        setInterval(function(){
            var new_thumb_url = thumb_url.replace(/\d{1,2}\.jpg/gi, i + '.jpg');
            console.log(new_thumb_url);
            //$(this).css('background-image', new_thumb_url);
            $this.css('background-image', new_thumb_url);
            i++;
            if(i > 10) i = 1;
        }, 3000);
    }
});

快乐编码:)

在您的
setInterval
$(此)
中,可能有一些东西超出了您的预期。是的,您是对的。那么我如何将参数传递给setInterval函数呢?那么当前代码发生了什么?我想您可以添加另一个变量
var target
或其他什么,并在
setInterval
函数中使用它。@putvande:谢谢你的兄弟,我没有注意到这一点。