繁体   English   中英

页面刷新javascript后检测div变化

[英]Detect div change after page refresh javascript

我有一个页面显示一个带有数字的表格。 这些是我的合作伙伴在商店看到的价格。 这个页面是专门为他制作的。

价格经常变动。 我在 WordPress 中输入价格。 我创建了自定义帖子类型和一个简单的表格来输入这些数字。 一个价格 = 一个自定义字段。 字段数约为 30。

而且效果很好。 但我需要检测价格变化。 我希望我的合作伙伴的网站清楚地显示价格变化。

理想的情况是当新价格的 DIV 颜色发生变化时,它旁边会出现一个按钮来重置其颜色。 这样的机制可以让您快速了解价格变化。 此页面每 10 秒刷新一次。

我知道JS的基础知识,但我不知道该怎么做。 我怀疑您将需要使用 SessionStorage。 谁能给我指示或粘贴类似解决方案的链接?


我有很多 div,例如:

<div id="price1">[types field='price1'][/types]</div>

和JS:

setTimeout(function(){
   window.location.reload(1);
}, 10000);

var price1 = document.getElementById('price1').textContent;

sessionStorage.setItem("price1-box", "price1");

var handle_storage = function () {
        console.log('change in storage! new' + price1);
      };

window.addEventListener("storage", handle_storage, false);

我做了 o 解决方案。 也许它对其他人有用。

<div id="eur-k">123</div>


window.onload = function() {

    setTimeout(function(){
       window.location.reload(1);
    }, 30000);

    // define div to add or remove alert
    var eurkDiv = document.getElementById('eur-k');

    // store current content in a variable
    var eurk = eurkDiv.textContent;

    // compare local storage with current content to make alarm
    if (localStorage.getItem('eurkLoc') != eurk) {
        eurkDiv.classList.add("active");
        eurkDiv.innerHTML = eurk + "<button type=\"button\" class=\"potwierdzenie\" onclick=\"resetFunction()\">OK</button>";
    }

    function resetFunction() {
        eurkDiv.classList.remove("active");
        eurkDiv.textContent = eurk;
        localStorage.setItem('eurkLoc', eurk);
    };
};

暂无
暂无

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

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