开发者

Set HTML Page background image using jQuery

开发者 https://www.devze.com 2023-03-10 17:30 出处:网络
I want to set page backgroudImage using jQuery. I wrote this code but it is not work. What is the problem?

I want to set page backgroudImage using jQuery.

I wrote this code but it is not work.

What is the problem?

Image is exist

Set HTML Page background image using jQuery

var Page = $(this);
$(function () {
    SetBackgroundImage();
});


function SetBackgroundImage() {
    //Todo read ImagePath from ser开发者_JAVA技巧ver

    var ImageUrl;
    ImageUrl ="../Images/BackgroudImage.jpg";

    try {

        Page.css('background-image', 'url(' + ImageUrl + ')');
    } catch (e) {
        //LogError
        alert(e.Description);
    }

} 


The variable Page currently holds a reference to a jQuery object which is wrappend around the window element. You'll want the jQuery object to wrap around the body tag.

To retrieve the body element you'll have to adjust your code a bit, like so:

$(function() {
    $('body').css('background-image', 'url(' + ImageUrl + ')');
});

I suggest you take a look at the jQuery API for some more information about jQuery.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号