jquery dynamic Replacement Setting background image method _jquery

Source: Internet
Author: User

Sometimes, we can provide users with very intimate functions, such as to determine when the user is to visit, and then give a greeting, good evening, good afternoon and so on. and change the background color of the Web page, such as night when you can use the background of the stars, daytime with the sun shining, or a specific holiday on the theme of the background, so that your site appears very flexible, not boring.

Here is a workaround for how to implement a background change:

How to Achieve

Very simply, here's the JQuery code:

Copy Code code as follows:

function dochangebkg ()
{
var bkgurl=$ ("#inputBkgUrl"). Val ();
var repeatemode=$ ("#inputRepeatMode"). Val ();
var s= "<style type= ' text/css ' > body{background-repeat:" +repeatemode+ "; Background-image:url (\" "+bkgUrl+" \ ");} </style> ";
$ ("#outputDiv"). HTML (s);
}

Native JavaScript code:

Copy Code code as follows:

function dochangebkg ()
{
var bkgurl=$ ("#inputBkgUrl"). Val ();
var repeatemode=$ ("#inputRepeatMode"). Val ();
var s= "<style type= ' text/css ' > body{background-repeat:" +repeatemode+ "; Background-image:url (\" "+bkgUrl+" \ ");} </style> ";
$ ("#outputDiv"). HTML (s);
}

Front-End Code:

Copy Code code as follows:

<div>
Background picture url:<input type= "text" size= "a" id= "Inputbkgurl"/> <br,/>
The background picture repeats the way: <select id= "Inputrepeatmode" ><option value= "Repeat" selected= "selected" >repeat</option ><option value= "no-repeat" >no-repeat</option><option value= "Repeat-x" >repeat-x</option ><option value= "Repeat-y" >repeat-y</option></select>
<input type= "button" onclick= "dochangebkg ()" value= "Confirm replacement"/>
</div>
<div id= "Outputdiv" ></div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.