gpt4 book ai didi

javascript - 页面加载后仅重新加载一个(切换)文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:41 26 4
gpt4 key购买 nike

我必须在页面加载后(通过选择)在多个文件(用户主题选择)之间切换。

不幸的是,我不知道该怎么做。

我想做的是这样的:

betw: 我使用 Polymer.js

脚本:

 document.addEventListener('select-theme', function(e){
// The event is fired from a nested component.

console.log('select-theme: ', e.detail.themeValue)
var theme = e.detail.themeValue;
var importTag = document.getElementById('themeImport');
var style = document.getElementById('style');
var stylePath;
var importPath;

if(theme == "Green"){
importPath = "../app-theme-green.html";
stylePath ="app-theme-green";
}else{
importPath = "../app-theme-default.html";
stylePath ="app-theme-default";
}

importTag.setAttribute('href', importPath);
style.setAttribute('include', stylePath);
//Load new file
});

HTML

<link id="themeImport" rel="import" href="../app-theme-green.html">

<template is="dom-bind" id="app">
<style id="style" is="custom-style" include="app-theme-green"></style>
// ... some content
</template>

这可能吗?

帮助将不胜感激:-)

最佳答案

我关注了this让它工作的答案,所以我改变了我的代码的几个部分。

我现在使用 .ccs 文件而不是使用 html dom 模块。

document.addEventListener('select-theme', function(e){
console.log('select-theme: ', e.detail.themeValue)
var theme = e.detail.themeValue;
var importPath;

if(theme == "Green"){
importPath = "../app-theme-green.css";
}else{
importPath = "../app-theme-default.css";
}

var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = importPath;
link.media = 'all';
head.appendChild(link);
});

关于javascript - 页面加载后仅重新加载一个(切换)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com