简体   繁体   English

如何使用javascript或jquery添加和删除外部CSS

[英]How to add and remove external css using javascript or jquery

I am working with my webpage and I am using an external css, what I want is when the browser resize to it's minimum size, the pre-loaded stylesheet will remove and replace it with the new one which is for the minimum size of the browser. 我正在使用我的网页,并且正在使用外部CSS,我想要的是当浏览器调整为最小尺寸时,预加载的样式表将被删除,并替换为新的浏览器最小尺寸。 But I don't know how to do that stuff. 但是我不知道该怎么做。 Can someone help me? 有人能帮我吗?

Your best bet in this situation is to use CSS media queries , eg: 在这种情况下,最好的选择是使用CSS媒体查询 ,例如:

@media (max-width: 200px) {
    /* ...styles to apply up to 200 wide... */
    body {
        /* ... */
    }
}

@media (min-width: 201px) {
    /* ...styles to apply when more than 200 wide... */
    body {
        /* ... */
    }
}

However, answering the question you actually asked: 但是,回答您实际提出的问题:

The stylesheet added by a style or link element will be removed if you remove the element. 如果删除元素,则将删除由stylelink元素添加的样式表。 So you can do: 因此,您可以执行以下操作:

$("selector for the old stylesheet").remove();

...to remove the old, then ...删除旧的,然后

$("<style>...</style>").appendTo('head');

...to add another (or, of course, a link element instead). ...添加另一个(当然,或者添加一个link元素)。

You can trigger the process using the resize event on the window : 您可以使用window上的resize事件触发该过程:

$(window).on("resize", function() {
    // ...do something
});

Hi for my is very easy use a media queries on CSS 嗨,我很容易在CSS上使用媒体查询

With mediaQueries in your file of css can manipulate the sizes of his elements depending on the device where it is displayed. 使用css文件中的mediaQueries,可以根据显示元素的设备来操纵其元素的大小。

for example: 例如:

@media screen and(min-width:780px) and (max-width:1024px){Here the CSS for PC}

@media screen and(min-width:480px) and (max-with:780px){ Here the CSS for Tablets}

@media screen and(max-width:480px){ for Cellphones}

This can be done in a couple of lines of jQuery: 这可以通过几行jQuery完成:

$(document).ready(function () { $(document).ready(function(){

 $("a").click(function () { $('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />'); }); 

The key is at what time we add the link to the style sheet. 关键是什么时候我们将链接添加到样式表。 The first line in the code above repeated here asserts that the DOM is ready for manipulation. 上面重复的代码中的第一行(此处重复)断言DOM已准备好进行操作。

$(document).ready(function () { $(document).ready(function(){

 //... 

}); });

The second part repeated here adds a click event to taget Element in the page. 此处重复的第二部分将click事件添加到页面中的taget元素。

in to html: 进入html:

<link rel="stylesheet" href="simple.css" media="screen and (min-width:500px)">

link media 链接媒体

in to css: 进入css:

@media (min-width: 500px) and (max-width: 600px) {
        h1 {
          color: fuchsia;
        }

        .desc:after {
          content:" In fact, it's between 500px and 600px wide.";
        }
      }

media query 媒体查询

with jquery: 使用jQuery:

(function($){
    $(window).resize(function(){
        if($(this).width()>500){
            $('link[rel=stylesheet][href~="foo.com"]').remove();
            $('<link rel="stylesheet" href="styleMax.css"/>').appendTo('head');
        }
    });
}($));

jquery resize() jQuery resize()

with javascript 用JavaScript

html html

<link id="minCSS" rel="stylesheet" href="stylemin.css">
<link id="medCSS" rel="stylesheet" href="stylemed.css" disabled>
<link id="maxCSS" rel="stylesheet" href="stylemax.css" disabled>
<link id="defaultCSS" rel="stylesheet" href="defaultCSS.css" disabled>

javascript javascript

window.onresize=function(){
    if(this.innerWidth>500){
        document.getElementById("minCSS").disabled = true;
        document.getElementById("medCSS").removeAttribute('disabled');
    } else {
        document.getElementById("defaultCSS").removeAttribute('disabled');
    }
};

onresize 调整大小

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

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