【问题标题】:Change (content) text in div element once the class changes on body一旦类在 body 上更改,更改 div 元素中的(内容)文本
【发布时间】:2020-01-16 10:48:13
【问题描述】:

我只需要小的 jquery 帮助。所以我们想改变左边元素的文本值。所以一旦用户滚动到第二张幻灯片,数字 (1) 应该更改为数字 (2) .

滚动后,body 类将更改为: fp-viewing-1

这里的网站视频: https://drive.google.com/file/d/10OcUoyvh1Xr3pWmxqzdR67wI8IlkPsNF/view?usp=sharing

这是我尝试过的:

<script>
$(document).ready(function() {
if ($('body').attr("class") == "fp-viewing-1") {
         $('#count').addClass('something');
  }
 });
</script>

添加类后,我想用 css 更改 html 值。

 content: "";

【问题讨论】:

标签: javascript jquery css


【解决方案1】:
// you can use two Intervals to detect as there is no event for class change in Jquery  
$(document).ready(function () {
        startProcess();
    });
    function startProcess() {
        var checkExist1 = setInterval(function () {
            if ($('body').hasClass("fp-viewing-1")) {
                clearInterval(checkExist1);
                alert("1 detected")
                var checkExist0 = setInterval(function () {
                    if ($('body').hasClass("fp-viewing-0")) {
                        clearInterval(checkExist0);
                        startProcess();
                        alert("0 detected")
                    }
                }, 100);
            }
        }, 100);
    }

【讨论】:

  • 我们可以使用Mutation Observer来代替间隔。哪种方式更合适、更可靠、更高效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2014-10-19
  • 2021-10-08
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多