英文:
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')
,但效果不大
如果您决定提供帮助,提前感谢!
英文:
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 = $('#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");
}
});
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('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");
}
});
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('change')
in the second script but it didn't do much
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){
$("html").removeClass("dark light").addClass(theme);
}
Then give your iframe an ID if it doesn't already.
ie <iframe ..... id="chatBox"></div>
Then in the parent you can call that function like this:
const chatBox = document.querySelector("#chatBox");
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论