如何使用JavaScript选择css文件?

如何使用JavaScript选择css文件?,javascript,html,css,Javascript,Html,Css,我想有两个不同的css文件,一个用于window.devicePixelRatio,一个用于高dpi设备 如何使用JavaScript选择css 到目前为止我所做的(没有工作): if(window.devicePixelRatio 我不太确定,但是 <head> <link id="theCSS" href="" rel="stylesheet" type="text/css" /> <script> if (window.devicePixe

我想有两个不同的css文件,一个用于window.devicePixelRatio,一个用于高dpi设备

如何使用JavaScript选择css

到目前为止我所做的(没有工作):


if(window.devicePixelRatio

我不太确定,但是

<head>
 <link id="theCSS" href="" rel="stylesheet" type="text/css" />
<script>
    if (window.devicePixelRatio <= 1) {
          document.getElementById('theCSS').href= "style.css";
        } else {
         document.getElementById('theCSS').href= "stylemobi.css";
     }
</script>
</head>


如果(window.devicePixelRatio)为css
元素提供了一个id,并在js中使用,
document.getElementById()
获取它,然后根据您的条件将它分配给您想要的css,我不太确定,但如何

<head>
 <link id="theCSS" href="" rel="stylesheet" type="text/css" />
<script>
    if (window.devicePixelRatio <= 1) {
          document.getElementById('theCSS').href= "style.css";
        } else {
         document.getElementById('theCSS').href= "stylemobi.css";
     }
</script>
</head>


如果(window.devicePixelRatio)为css
元素提供了一个id,并在js中使用,
document.getElementById()
获取它,然后根据您的条件将它分配给您想要的css,我不太确定,但如何

<head>
 <link id="theCSS" href="" rel="stylesheet" type="text/css" />
<script>
    if (window.devicePixelRatio <= 1) {
          document.getElementById('theCSS').href= "style.css";
        } else {
         document.getElementById('theCSS').href= "stylemobi.css";
     }
</script>
</head>


如果(window.devicePixelRatio)为css
元素提供了一个id,并在js中使用,
document.getElementById()
获取它,然后根据您的条件将它分配给您想要的css,我不太确定,但如何

<head>
 <link id="theCSS" href="" rel="stylesheet" type="text/css" />
<script>
    if (window.devicePixelRatio <= 1) {
          document.getElementById('theCSS').href= "style.css";
        } else {
         document.getElementById('theCSS').href= "stylemobi.css";
     }
</script>
</head>


如果(window.devicePixelRatio)为css
元素提供了一个id,并在js中使用,
document.getElementById()
获取它,并根据您的条件将它分配给您想要的css,则使用一个link标记并给它一个id,然后在它放置一个脚本以设置它的href

<link id="main-style" rel=...>
<script>
   var stylesheet = window.devicePixelRatio <= 1 :'styles.css' : 'stylemobi.css';
   document.getElementById('main-style').setAttribute('href',stylesheet ); 
</script>


var stylesheet=window.devicePixelRatio使用一个link标记并给它一个ID,然后在它后面放一个脚本来设置它的href

<link id="main-style" rel=...>
<script>
   var stylesheet = window.devicePixelRatio <= 1 :'styles.css' : 'stylemobi.css';
   document.getElementById('main-style').setAttribute('href',stylesheet ); 
</script>


var stylesheet=window.devicePixelRatio使用一个link标记并给它一个ID,然后在它后面放一个脚本来设置它的href

<link id="main-style" rel=...>
<script>
   var stylesheet = window.devicePixelRatio <= 1 :'styles.css' : 'stylemobi.css';
   document.getElementById('main-style').setAttribute('href',stylesheet ); 
</script>


var stylesheet=window.devicePixelRatio使用一个link标记并给它一个ID,然后在它后面放一个脚本来设置它的href

<link id="main-style" rel=...>
<script>
   var stylesheet = window.devicePixelRatio <= 1 :'styles.css' : 'stylemobi.css';
   document.getElementById('main-style').setAttribute('href',stylesheet ); 
</script>


var stylesheet=window.devicePixelRatio根本不使用JS,只需在
元素中添加一些
媒体属性即可检查:



否则,如果你想使用JavaScript,你必须编写有效的JavaScript。你不能混用JS和HTML。

根本不使用JS,只需在
元素中添加一些
媒体
属性来检查:



否则,如果你想使用JavaScript,你必须编写有效的JavaScript。你不能混用JS和HTML。

根本不使用JS,只需在
元素中添加一些
媒体
属性来检查:



否则,如果你想使用JavaScript,你必须编写有效的JavaScript。你不能混用JS和HTML。

根本不使用JS,只需在
元素中添加一些
媒体
属性来检查:


否则,如果你想使用JavaScript,你必须编写有效的JavaScript。你不能只是混合使用JS和HTML。

谢谢你的回答! 我现在使用这种方法,在一个css文件中使用不同的css样式,根据显示ppi自动选择

body {
    margin: 0;
    padding: 0;
    /*background-color:#008B09;*/
    background-image:url("purty_wood.jpg");
    background-color:#008B09;
    font-family: 'ABeeZee', sans-serif;
    color:#000000;
    overflow:auto;
    font-size:1.2em;
}


@media (-webkit-min-device-pixel-ratio: 2), 
    (min-resolution: 192dpi) {
        body {
            font-size:2em;
        }
    }
在这种情况下,如果分辨率超过192ppi或dp比率超过2,浏览器会自动覆盖标准css。因此,在单个css文件中,我可以为不同的显示设置不同

(我只需要将分辨率设置为最小235,因为有些mac显示器的分辨率高达230 ppi,而大多数智能手机的分辨率至少为250 ppi。)

更多信息:

谢谢您的回答! 我现在使用这种方法,在一个css文件中使用不同的css样式,根据显示ppi自动选择

body {
    margin: 0;
    padding: 0;
    /*background-color:#008B09;*/
    background-image:url("purty_wood.jpg");
    background-color:#008B09;
    font-family: 'ABeeZee', sans-serif;
    color:#000000;
    overflow:auto;
    font-size:1.2em;
}


@media (-webkit-min-device-pixel-ratio: 2), 
    (min-resolution: 192dpi) {
        body {
            font-size:2em;
        }
    }
在这种情况下,如果分辨率超过192ppi或dp比率超过2,浏览器会自动覆盖标准css。因此,在单个css文件中,我可以为不同的显示设置不同

(我只需要将分辨率设置为最小235,因为有些mac显示器的分辨率高达230 ppi,而大多数智能手机的分辨率至少为250 ppi。)

更多信息:

谢谢您的回答! 我现在使用这种方法,在一个css文件中使用不同的css样式,根据显示ppi自动选择

body {
    margin: 0;
    padding: 0;
    /*background-color:#008B09;*/
    background-image:url("purty_wood.jpg");
    background-color:#008B09;
    font-family: 'ABeeZee', sans-serif;
    color:#000000;
    overflow:auto;
    font-size:1.2em;
}


@media (-webkit-min-device-pixel-ratio: 2), 
    (min-resolution: 192dpi) {
        body {
            font-size:2em;
        }
    }
在这种情况下,如果分辨率超过192ppi或dp比率超过2,浏览器会自动覆盖标准css。因此,在单个css文件中,我可以为不同的显示设置不同

(我只需要将分辨率设置为最小235,因为有些mac显示器的分辨率高达230 ppi,而大多数智能手机的分辨率至少为250 ppi。)

更多信息:

谢谢您的回答! 我现在使用这种方法,在一个css文件中使用不同的css样式,根据显示ppi自动选择

body {
    margin: 0;
    padding: 0;
    /*background-color:#008B09;*/
    background-image:url("purty_wood.jpg");
    background-color:#008B09;
    font-family: 'ABeeZee', sans-serif;
    color:#000000;
    overflow:auto;
    font-size:1.2em;
}


@media (-webkit-min-device-pixel-ratio: 2), 
    (min-resolution: 192dpi) {
        body {
            font-size:2em;
        }
    }
在这种情况下,如果分辨率超过192ppi或dp比率超过2,浏览器会自动覆盖标准css。因此,在单个css文件中,我可以为不同的显示设置不同

(我只需要将分辨率设置为最小235,因为有些mac显示器的分辨率高达230 ppi,而大多数智能手机的分辨率至少为250 ppi。)


更多信息:

无法将脚本放在元素之前,脚本运行时元素不存在runs@charlietfl当然,让我编辑..谢谢,那是个错误。不能将脚本放在元素之前,当脚本出现时元素不存在runs@charlietfl当然,让我来编辑..谢谢,那是个错误。不能将脚本放在元素之前,元素不会当脚本runs@charlietfl当然,让我编辑..谢谢,那是个错误。不能将脚本放在元素之前,当脚本出现时元素不存在runs@charlietfl当然,让我来编辑……谢谢,那是个错误。