简体   繁体   English

更改div上多个页面的CSS背景图像?

[英]Change css background image on a div for multiple pages?

I have this CSS: 我有这个CSS:

div[data-role="page"]{ 
min-height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
border: 0px;
background-image:url('../img/blue_background.jpg');}

and this is my function: 这是我的功能:

Note: on the variables initialized in the function, the val() is from a selectbox that when changed, is saved in localStorage. 注意:在函数中初始化的变量上,val()来自选择框,更改后将保存在localStorage中。

function saveParam(){
var result=$("#param_results option:selected").val();
var scan=$("#param_scan option:selected").val();
var scan_speed=$("#param_scan_speed option:selected").val();
var scan_bg=$("#param_scan_bg option:selected").val();
var font=$("#param_font option:selected").val();
var screen_bg=$("#param_screen_bg option:selected").val();
var waiting_time=$("#param_waiting_time option:selected").val();

window.localStorage.setItem("param_results", result);
window.localStorage.setItem("param_scan", scan);
window.localStorage.setItem("param_scan_speed", scan_speed);
window.localStorage.setItem("param_scan_bg", scan_bg);
window.localStorage.setItem("param_font", font);
window.localStorage.setItem("param_screen_bg", screen_bg);
window.localStorage.setItem("param_waiting_time", waiting_time);


//$('div[data-role="page"]').css("background-image", "url('../img/green.jpg')");

back();}

I have several html pages, all of them have the div[data-role="page"] so all of them have the same background, etc... but when param_screen_bg is changed, I want to permanently change the background image to another but it's not working. 我有几个html页面,每个页面都有div [data-role =“ page”],所以所有页面都有相同的背景,等等...但是当param_screen_bg更改时,我想将背景图像永久更改为另一个但它不起作用。 The comment line at the end it's just my attempt to try to change the background but it's still not working. 最后的注释行只是我尝试更改背景的尝试,但仍然无法正常工作。

EDIT: https://jsfiddle.net/42zbcbge/1/ 编辑: https//jsfiddle.net/42zbcbge/1/

on this jsfiddle is a small example of what i want. 在这个jsfiddle上是我想要的一个小例子。 when you press the save button, i want to change the background on the css and therefor on the page too but it's not changing 当您按保存按钮时,我也想更改CSS上的背景,因此也要更改页面上的背景,但是它没有改变

Hey i didn't completely understood the code which you have added. 嘿,我不完全了解您添加的代码。 But, i guess you are using selectbox and passing image url in option's value. 但是,我想您正在使用selectbox并在option的值中传递图像url。 So while fetching, you should only use the id of selectbox, like : 因此,在提取时,您应该仅使用selectbox的ID,例如:

var screen_bg=$("#param_screen_bg").val();

instead of 代替

var screen_bg=$("#param_screen_bg option:selected").val();

var bg_image_value = localStorage.getItem("param_screen_bg");

if(bg_image_value != null && bg_image_value != '') {
   $('div[data-role="page"]').css("background-image", 'url('+bg_image_value+')' ); 
}

May be try adding code in codepen or jsfiddle. 可以尝试在Codepen或jsfiddle中添加代码。

You're setting the values in the localStorage, but you never seem to get 'm. 您正在localStorage中setting值,但您似乎从未get 'm。 So, you could, for example, do something like this: 因此,您可以例如执行以下操作:

jQuery().ready(function() //do when page is loaded
{
  if(window.localStorage.getItem("param_screen_bg") !== null) //check if value is set in localStorage
  {
    //get value from localStorage and apply it to the div:
    $('div[data-role="page"]').css("background-image", window.localStorage.getItem("param_screen_bg"));
  }
});

I'm making some assumptions in this sample though, but I think you can handle it ;) 尽管我在此示例中做出了一些假设,但我认为您可以处理;)

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

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