简体   繁体   English

在html文件中有2个CSS代码块,如何仅使用JS进行一项工作?

[英]Having 2 css code blocks in html files how to make only one work using JS?

So we have 1 html file (required). 因此,我们有1个html文件(必需)。 In there 2 css style blocks and one JS block. 其中有2个CSS样式块和1个JS块。 how to make it possible to set to use one of the styles from JS? 如何使其能够设置为使用JS中的一种样式? (I need it to work in IE6 IE7 IE8 FF and chrome 9 =) I need to give one css for IE9 and Chrome 8 and up and another CSS for all others. (我需要它在IE6 IE7 IE8 FF和chrome 9中起作用)我需要为IE9和Chrome 8及更高版本提供一个CSS,为所有其他提供CSS。

<style type="text/css" >
H1 {font-size: 24pt; font-family: arial}
</style>

<style type="text/css">
H1 {font-size: 15pt;}
</style>

Use conditional comments to target IE6 specifically, like this: 使用条件注释专门针对IE6,如下所示:

<!--[if IE 6]>
<style type="text/css">
  H1 {font-size: 15pt;}
</style>
<![endif]-->

Or the other way around, whichever it is..but whatever's inside these comments will only apply to IE6. 或反之亦然,无论如何..但是这些注释中的内容适用于IE6。 No JavaScript is required for the above approach :) 上述方法不需要JavaScript :)

For an alternative way involving JS and handing chrome. 另一种涉及JS和处理chrome的方法。 (I learned this form ExtJs but I'm not sure where it originated.) (我了解了这种形式的ExtJs,但不确定它起源于何处。)

Load a browser detection script. 加载浏览器检测脚本。 PPK's is pretty good: http://www.quirksmode.org/js/detect.html . PPK相当不错: http : //www.quirksmode.org/js/detect.html

Add classes to document.body. 将类添加到document.body。

document.body.className += ' ' + BrowserDetect.browser 
  + ' ' + BrowserDetect.browser + '-' + BrowserDetect.version;

Now write your css like this 现在像这样写你的CSS

H1 {
  font-size: 24pt; 
  font-family: arial;
}

.Explorer-6 H1 {
  font-size: 15pt;
}

.Chrome-9 H1 {
  /* ... */
}

You could mix the OS in there too, but I'm not sure you'll ever need it. 您也可以在其中混合使用操作系统,但是我不确定您是否会需要它。

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

相关问题 如何使 js 与特定的 html 块一起工作? - How to make js work with specific html blocks? 如何仅使用 HTML 和 CSS(无 JS)制作“粘性”导航栏? - How to make a “sticky” navbar using only HTML and CSS (without JS)? 如何使我的.css和.js文件与我在Android上的WebView中加载的html代码一起使用 - How to make my .css and .js file work with my html code loaded in a WebView on Android 如何使css类一次仅对一个对象起作用? - How to make a css class work for only one object at a time? 如何部署到 vercel static 文件? 只有 html、css 和 js - How to deploy to vercel static files? only html, css and js 如何将版本号添加到HTML文件(不仅是CSS和JS文件) - How to add version number to HTML file (not only to css and js files) 我如何使这个 HTML 风格的计算器只能使用 JS 工作 - How do i make this HTML styled calculator work only using JS 如何在HTML CSS Bootstrap或angular js中制作编程代码框 - How to make a programming code box in html css bootstrap or angular js JS:如何确保只有一个 html 元素可见 - JS: how to make sure only one html element is visible 如何使用CSS和JS编写的多选代码仅选择一个选项? 目前,我可以同时选择 - How do I make my multiple choice code written in CSS & JS to only select one option? Currently I can select both
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM