Jquery document.body.background

Jquery document.body.background

我在使用 jquery 脚本时遇到了一些问题,该脚本应该在按下 link 时更改我的 body 元素的背景图像。它工作得很好,我遇到的唯一问题是该页面必须在 CSS 中没有设置背景,并且在按下任何 link 之前为空白,这会更改背景。使用的 jquery 确实很旧,尝试过较新的版本,但除了这个以外没有任何效果。另外,我试过 document.body.style.backgroundImage ,仍然没有成功,这可能是因为这个脚本使用的是旧版本。你能找出这里出了什么问题吗?

  <script language="JavaScript">
    <!--

    // Copyright 2001 by www.CodeBelly.com
    // Please do *not* remove this notice.

    var backImage = new Array(); // don't change this

    // Enter the image filenames you wish to use.
    // Follow the pattern to use more images.  The
    // number in the brackets [] is the number you
    // will use in the function call to pick each
    // image.

    // Note how backImage[3] = "" -- which would
    // set the page to *no* background image.

    backImage[0] = "img/1.jpg";
    backImage[1] = "img/2.jpg";
    backImage[2] = "img/3.jpg";
    backImage[3] = "img/4.jpg";
    backImage[4] = "img/5.jpg";
    backImage[5] = "img/3.jpg";
    backImage[6] = "img/4.jpg";
    backImage[7] = "img/5.jpg";
    backImage[8] = "img/3.jpg";
    backImage[9] = "img/1.jpg";
    //-----------------------------

    function changeBGImage(whichImage){
    if (document.body){
    document.body.background = backImage[whichImage];

    }
    }

    //-->
    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">
    </script>

试试这个

$('body').css('background-image','url("source of your image")');

通过 JS 本身,试试这个:

document.body.style.backgroundImage = 'url('+backImage[index]+')'

以下行代码应该适合您:

$('body').css('background-image', 'url(' + backImage[whichImage] + ')');