简体   繁体   English

根据ID加载css样式表?

[英]Load css stylesheet based on ID?

Hey how can I load a css stylesheet only based on id using javascript? 嘿我怎么能只使用javascript基于id加载css样式表? Here is the code that I have: 这是我的代码:

<link id='bs-css' rel='stylesheet' href='bootstrap-cyborg.css' type='text/css' />

I want to be able to use it only on the div that I want so something similar to this code: 我希望能够只在我想要的div上使用它,所以类似于这段代码:

<div id="bs-css"> 
some code 
</div>

I tried this code but its not functioning like I wanted, here is the code: 我试过这段代码,但它的功能不像我想要的,这里是代码:

<script type="text/javascript"> 
$(document).ready(function(){

if($("#bs-css").size()>0){
        if (document.createStyleSheet){
            document.createStyleSheet('bootstrap-cyborg.css');
        }
        else {
            $("head").append($("<link id='bs-css' rel='stylesheet' href='bootstrap-cyborg.css' type='text/css' />"));
        }
    }
});
</script> 

How can I load a css stylesheet only on the div that I want using an id and javascript? 如何只在我想要使用id和javascript的div上加载css样式表?

Stylesheets are not bound to element IDs. 样式表未绑定到元素ID。 CSS declarations within the stylesheet are. 在样式表 CSS声明 There is little point attempting to load a stylesheet in code as you propose. 尝试在代码中加载样式表没有什么意义。

Simply making a declaration for the ID you are targetting within a typical stylesheet is the best way to do this. 简单地在典型样式表中声明您要识别的ID是最好的方法。

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

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