如何在索引页面上设置复选框状态并在 iframe 中切换内容

huangapple go评论60阅读模式
英文:

how to set checkbox status on index page and toggle content in an iframe

问题

我正在尝试创建一个在暗模式和亮模式之间切换的开关;但是我在让我的iframe跟随切换方面遇到了问题...

这是我的当前jQuery代码:

$(document).ready(function() {
  var switched = $('#mode');
  
  switched.on("change", function(){
    if (switched.is(':checked')){
      $('html').removeClass('dark');
      $('html').addClass('light');
      localStorage.setItem('mode','light');
    }
    else{
      $('html').removeClass('light');
      $('html').addClass('dark');
      localStorage.setItem('mode','dark');
    }
  });
  var mode = localStorage.getItem('mode');
  if (mode == 'dark') {
    switched.prop( "checked", false );
    $('html').removeClass("light");
    $('html').addClass("dark");
  }
  if (mode == 'light') {
    switched.prop( "checked", true );
    $('html').removeClass("dark");
    $('html').addClass("light");
  }

});

我的代码获取具有 #mode ID 的复选框的状态,并向 :root 添加/移除相应的类。然后,使用以下CSS,开关可以工作:

:root,
:root.dark {
  --color-bg: #263238;
  --color-fg: #ffffff;
}
:root.light {
  --color-bg: #ffffff;
  --color-fg: #000000;
}

但是在我的首页上,我嵌入了一个位于 <object> 中的聊天框,所以我还编写了这段代码,可以应用于在iframe中的聊天框页面:

$.get('https://amy-testfo.forumactif.com/', function(datas){ 
    var coche = $('#mode',datas);
    var mode = localStorage.getItem('mode');

    if(coche.not(':checked')){
      $('html').removeClass("light");
      $('html').addClass("dark");
    }
    if(coche.is(':checked')){
      $('html').removeClass("dark");
      $('html').addClass("light");
    }

    if (mode == 'dark') {
      $('html').removeClass("light");
      $('html').addClass("dark");
    }
    if (mode == 'light') {
      $('html').removeClass("dark");
      $('html').addClass("light");
    }

  });

但是当我选中复选框时,聊天框仍然保持在暗模式,需要重新加载页面才能实际切换,这就是我的问题。我尝试在第二个脚本中使用 .on('change'),但效果不大 如何在索引页面上设置复选框状态并在 iframe 中切换内容

如果您决定提供帮助,提前感谢!

英文:

i'm trying to build a toggle between dark & light mode; but i'm having trouble getting my iframes to follow the switch...

Here's my current jquery:

$(document).ready(function() {
  var switched = $(&#39;#mode&#39;);
  
  switched.on(&quot;change&quot;, function(){
    if (switched.is(&#39;:checked&#39;)){
      $(&#39;html&#39;).removeClass(&#39;dark&#39;);
      $(&#39;html&#39;).addClass(&#39;light&#39;);
      localStorage.setItem(&#39;mode&#39;,&#39;light&#39;);
    }
    else{
      $(&#39;html&#39;).removeClass(&#39;light&#39;);
      $(&#39;html&#39;).addClass(&#39;dark&#39;);
      localStorage.setItem(&#39;mode&#39;,&#39;dark&#39;);
    }
  });
  var mode = localStorage.getItem(&#39;mode&#39;);
  if (mode == &#39;dark&#39;) {
    switched.prop( &quot;checked&quot;, false );
    $(&#39;html&#39;).removeClass(&quot;light&quot;);
    $(&#39;html&#39;).addClass(&quot;dark&quot;);
  }
  if (mode == &#39;light&#39;) {
    switched.prop( &quot;checked&quot;, true );
    $(&#39;html&#39;).removeClass(&quot;dark&quot;);
    $(&#39;html&#39;).addClass(&quot;light&quot;);
  }

});

My code fetches the state of a checkbox with the #mode id, and gives/removes the corresponding class to :root. The switch then works with CSS like this:

:root,
:root.dark {
  --color-bg: #263238;
  --color-fg: #ffffff;
}
:root.light {
  --color-bg: #ffffff;
  --color-fg: #000000;
}

But on my index page, i have a chatbox embedded in an html <object>, so I also wrote this bit of code, which i can apply to the chatbox page that's in the iframe:

$.get(&#39;https://amy-testfo.forumactif.com/&#39;, function(datas){ 
    var coche = $(&#39;#mode&#39;,datas);
    var mode = localStorage.getItem(&#39;mode&#39;);

    if(coche.not(&#39;:checked&#39;)){
      $(&#39;html&#39;).removeClass(&quot;light&quot;);
      $(&#39;html&#39;).addClass(&quot;dark&quot;);
    }
    if(coche.is(&#39;:checked&#39;)){
      $(&#39;html&#39;).removeClass(&quot;dark&quot;);
      $(&#39;html&#39;).addClass(&quot;light&quot;);
    }

    if (mode == &#39;dark&#39;) {
      $(&#39;html&#39;).removeClass(&quot;light&quot;);
      $(&#39;html&#39;).addClass(&quot;dark&quot;);
    }
    if (mode == &#39;light&#39;) {
      $(&#39;html&#39;).removeClass(&quot;dark&quot;);
      $(&#39;html&#39;).addClass(&quot;light&quot;);
    }

  });

But when I check the checkbox, the chatbox stays in dark mode and needs a page reload in order to actually switch, so that's my problem. I tried using .on(&#39;change&#39;) in the second script but it didn't do much 如何在索引页面上设置复选框状态并在 iframe 中切换内容

Thank you in advance if you decide to help!

答案1

得分: 2

如果您控制IFRAME和主页面,您可以在IFRAME页面中创建一个函数,然后从父页面调用它。

例如,在您的IFRAME中有一个名为toggleTheme的函数。在我的示例中,由于您有两个主题,所以与使用if语句相比,只需删除这两个类并添加一个类更容易。

function toggleTheme(theme){
   $("html").removeClass("dark light").addClass(theme);
}

然后,如果尚未为您的iframe指定ID,请为其添加一个ID:

<iframe ..... id="chatBox"></iframe>

然后,在父页面中,您可以像这样调用该函数:

const chatBox = document.querySelector("#chatBox");
chatBox.contentWindow.toggleTheme(theme);

document.querySelector("#chatBox") 将引用iframe本身。

contentWindow 将获取iframe的窗口对象。

toggleTheme 是您在上面创建的函数。

英文:

If you control both the IFRAME and the main page, you can create a function within the IFRAME page and call it from the parent.

For example, in your IFRAME have a function called: toggleTheme. In my example, since you have two themes, its easier to just remove both of those classes and add a class compared to if statements.

function toggleTheme(theme){
   $(&quot;html&quot;).removeClass(&quot;dark light&quot;).addClass(theme);
}

Then give your iframe an ID if it doesn't already.

ie &lt;iframe ..... id=&quot;chatBox&quot;&gt;&lt;/div&gt;

Then in the parent you can call that function like this:

const chatBox = document.querySelector(&quot;#chatBox&quot;);
chatBox.contentWindow.toggleTheme(theme);

document.querySelector("#chatBox") Will reference the iframe itself.

contentWindow will get the window object of the iframe.

toggleTheme is the function that you created from above

huangapple
  • 本文由 发表于 2023年7月11日 03:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定