简体   繁体   English

用jQuery更改背景图像(添加新图像路径)

[英]Changing background image (adding new image path) with Jquery

I have my custom bg already. 我已经有了我的自定义背景。 Also, I want to add a form field which will give to my visitors a choise to change it (they may add their custom BG url (any site) in to the form) + the result will store in the cookies. 另外,我想添加一个表单字段,这将使我的访问者可以选择更改它(他们可以在表单中添加其自定义BG网址(任何网站)),并且结果将存储在cookie中。 I know how to save any action in cookies, however I don't have any good example of how to change BG using form inputs. 我知道如何在Cookie中保存任何操作,但是我没有如何使用表单输入来更改BG的良好示例。

Could any one help me please? 有人可以帮我吗? I would apreciate any link on to a good example (which will support 1.9.1 version of Jquery) or your own example here. 我会在一个好的示例(它将支持1.9.1版本的Jquery)或您自己的示例上添加任何链接。 Thank you! 谢谢!

Try this one 试试这个

<input>
<button>done</button>
<div class="bg_img"></div>


$('button').click(function(){
 var bg_url = $('input').val();
    console.log(bg_url)
    $('.bg_img').attr('style','background-image:url('+bg_url+')');
});

here is jsfiddle 这是jsfiddle

using jQuery on page load: 在页面加载时使用jQuery:

$(document).ready(function() {
  $('body').attr('background-image', here is your cookie value where you store the image url);
});

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

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