简体   繁体   English

如何从另一个页面更改页面上 div 的颜色

[英]How do I change the color of div on a page from another page

Since I'm still learning the localStorage and cookie techniques, I am trying to change the color of the div on page1.html from page2.html by using the submit button, color of the div should be changed permanently when the user clicks submit button on page2.html由于我还在学习localStoragecookie技术,我想改变的div颜色page1.htmlpage2.html通过提交按钮,在div的颜色应永久当用户点击提交按钮改变在page2.html
This is what I got on page1:这是我在第 1 页上得到的:

window.onload = function() {
    var anchors = document.getElementsByClassName("circleBase type1");
    for(var i = 0; i < anchors.length; i++) {
        anchors[i].onclick = function() {
            window.open("EnterInformation.html");
        }
    }
}

on page2 i used localStorage for saving data permanently, with this save I need to change the color of the div on page1.在第 2 页上,我使用localStorage永久保存数据,通过此保存,我需要更改第 1 页上 div 的颜色。 This is page2 so far:到目前为止,这是第2页:

 function SaveInfo() {
        localStorage.Yritys = $('#Yritys').val();
        localStorage.Henkilönimi = $('#Henkilönimi').val();
        localStorage.Asema_yrityksessa = $('#Asema_yrityksessa').val();
        localStorage.Puhelin_Nr = $("#Puhelin_Nr").val();
        localStorage.e_Mail = $('#e_Mail').val();
        localStorage.Keskustelun_aihe = $('#Keskustelun_aihe').val();

What would be the solution to this?解决这个问题的方法是什么? It would be nice to use localStorage instead of cookies.使用localStorage而不是 cookie 会很好。 Thank You!谢谢你!

You can create a local storage您可以创建本地存储

myCustomColor = '#2B2A28';
localStorage.setItem('myDataStorage', myCustomColor);

Then retrieve them然后取回它们

var myLoadedColor = localStorage.getItem('myDataStorage');
document.getElementById('myDiv').style.backgroundColor = myLoadedColor;

myDataStorage is the name of your created localStorage. myDataStorage 是您创建的 localStorage 的名称。 You can use different names to create multiple localStorages if you would.如果愿意,您可以使用不同的名称来创建多个 localStorage。

You can .getItem on the localStorage upon page load and then set the color of your div based on that localStorage data.您可以在页面加载时在 localStorage 上使用 .getItem,然后根据该 localStorage 数据设置 div 的颜色。

var x = document.getElementsByClassName("example");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
}

This little piece of code scans your page for all elements with the matching class and changes the background color then.这段代码会扫描您的页面以查找具有匹配类的所有元素,然后更改背景颜色。 Of course you can use it also for single elements:当然,您也可以将它用于单个元素:

var x = document.getElementByClassName("example");
x.style.backgroundColor = "red";

But in this case JavaScript will only change the first div it finds with a matching class.但在这种情况下,JavaScript 只会更改它找到的具有匹配类的第一个 div。

To sum it up: There is no other convenient way than marking/tagging your elements in a way that they are unique and recognizable.总结一下:除了以独特且可识别的方式标记/标记元素之外,没有其他方便的方法。 Normally this is done by the name or the id.通常这是通过名称或 id 完成的。 Class would also work but is seldomly used in this way.类也可以工作,但很少以这种方式使用。

Well, actually you could also iterate through the DOM and count the elments in order to change let's say the 15th div that appears.好吧,实际上您也可以遍历 DOM 并计算元素以更改出现的第 15 个 div。 But what if you add additional divs a couple of days later?但是,如果您在几天后添加额外的 div 呢? It's a very uncomfortable approach.这是一种非常不舒服的方法。

Use the localStorage and it will work using names or ids.使用 localStorage,它将使用名称或 ID 工作。 You only have once to give every div it's id and/or name.您只需一次为每个 div 指定其 ID 和/或名称。

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

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