简体   繁体   English

使用 Javascript 切换 CSS 工作表(点击)

[英]Toggle CSS Sheets (on click) with Javascript

Trying to find code for switching between two style sheets with just one button.试图找到只需一个按钮即可在两个样式表之间切换的代码。 I've tried to adapt others' solutions, but to no avail (yet).我试图适应其他人的解决方案,但无济于事(还)。 Here's my most recent attempt:这是我最近的尝试:

Set Up:设置:

<link id="style1" rel="stylesheet" type="text/css" href="resumecss.css" />
<script type="text/javascript">
function toggle() {
    var el = document.getElementById("style1");
    if (el.href == "resumecss.css") {
        el.href = "resumecssinvert.css";    
    }
    else {
        el.href = "resumecss.css";  
    }
}
</script>

Calling:来电:

<button type="button" onclick="toggle()">Switch</button>

The purpose being to flip between two skins on one page repeatably.目的是重复地在一页上的两个皮肤之间翻转。

Thanks in advance to those of you kind/knowledgeable to help.在此先感谢你们这些善良/知识渊博的人。

Your script is solid, except that the href attribute is a full URL, even if you use a relative path. 您的脚本是可靠的,除非href属性是完整的URL,即使您使用相对路径也是如此。 Here's the code I used that works: 这是我使用的代码:

function toggle() {
    var el = document.getElementById("style1");
    if (el.href.match("resumecss.css")) {
        el.href = "resumecssinvert.css";    
    }
    else {
        el.href = "resumecss.css";  
    }
}

See fiddle here: http://jsfiddle.net/jakelauer/LWJjX/ 请看这里的小提琴: http//jsfiddle.net/jakelauer/LWJjX/

This is the shortest solution I could think of (and works probably in all browsers): 这是我能想到的最短的解决方案(并且可能在所有浏览器中都有效):

function toggle() {
  var a = document.getElementById("style1");
  a.x = 'resumecssinvert' == a.x ? 'resumecss' : 'resumecssinvert'; // short if
  a.href = a.x + '.css';
}

As everything in javascript is a object you can simply add properties. 由于javascript中的所有内容都是对象,因此您只需添加属性即可。

Assuming that your default css is " resumecss " 假设你的默认css是“ resumecss

The first time x is not set and returns false , so " resumecssinvert " will be set. 第一次没有设置x并返回false ,因此将设置“ resumecssinvert ”。

The second time time x is set and returns true and switches back. 第二次设置时间x并返回true并切换回。 Everything works as it should. 一切都按预期工作。

Try including both of them, and then switched the disabled flag 尝试包括它们,然后切换禁用标志

<link id="style1" rel="stylesheet" type="text/css" href="resumecss.css" />
<link id="style2" rel="stylesheet" type="text/css" href="resumecssinvert.css" disabled="disabled"/>
<script type="text/javascript">
function toggle() {
    var el1 = document.getElementById("style1"),
        el2 = document.getElementById("style2");
    if( el1.disabled == "disabled" ) {
        el1.disabled = undefined;
        el2.disabled = "disabled";
    } else {
        el1.disabled = "disabled";
        el2.disabled = undefined;
    }
}
</script>

this one works with Jquery. 这个适用于Jquery。

$('#dark-css').click(function() {

    var isDarkCss = $("link[href='dark.css']");

    if (isDarkCss.length){
        isDarkCss.remove();
    }else{
        $('head').append('<link type="text/css" rel="stylesheet" media="all" href="dark.css">');
    }
});

Add both stylesheets to the page, click on the button to disable one and the other is automatically active将两个样式表添加到页面,单击按钮禁用一个,另一个自动激活

let button =document.getElementById("IdName")
button.addEventListener("click", ()=>{
let stylesheet =document.getElementById("style1");
stylesheet.toggleAttribute("disabled")
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM