【问题标题】:Detect div change after page refresh javascript页面刷新javascript后检测div更改
【发布时间】:2019-02-12 22:57:09
【问题描述】:

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

价格经常变化。我在 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);

【问题讨论】:

  • 我看不到代码?请向我们展示您的代码并解释您遇到的确切错误。如果您无法做到这一点,那么您最好聘请开发人员。
  • 是的,sessionStorage 可能是最好的。但要检索最新价格,也许您想使用 ajax 调用来获取最新价格,然后填充价格表并确定单元格的颜色。

标签: javascript html reload


【解决方案1】:

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

<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);
    };
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 2016-08-04
    • 2011-05-16
    • 2018-05-02
    • 2012-01-04
    • 2018-05-27
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多