简体   繁体   English

JavaScript CSS选择器

[英]JavaScript CSS Selector

I was reading up on some JavaScript to try and create my own slider and I came across something that confused me a little. 我正在阅读一些JavaScript尝试创建自己的滑块,但遇到了一些让我感到困惑的东西。 There was a CSS rule that was as follows: 有一条CSS规则如下:

html.js #slideshow .slides img
{
    position: absolute;
}

The explanation said that the rule would only be applied if JavaScript was available. 说明说该规则仅在JavaScript可用时才适用。 Now I'm a bit confused... Would this rule be applied if JavaScript is available in the browser? 现在我有点困惑...如果浏览器中提供JavaScript,是否可以应用此规则? Or if the file "html.js" had been included in the html page, or if any JavaScript files were included? 还是在HTML页面中包含了文件“ html.js”,或者是否包含了JavaScript文件?

Thanks in advance. 提前致谢。

It's a technique where you use javascript to add class 'js' to the html tag. 这是一种使用javascript将类“ js”添加到html标签的技术。 No javascript, no js class. 没有javascript,没有js类。 Makes it very easy for your CSS to know if JS is enabled. 使CSS非常容易知道是否启用了JS。

Probably in your page you have a script on the head that adds a js class to the root element 可能在页面中您的头上有一个脚本,该脚本将js类添加到根元素

this will ensure that the CSS rule will be applied only if javascript is available on the client. 这样可以确保仅当客户端上有javascript时才应用CSS规则。 Of course if javascript is not available the class won't be inserted by the script. 当然,如果无法使用javascript,则脚本不会插入该类。

It's a modern approach used especially to prevent the flash of unstyled content (FOUC) 这是一种现代方法,专门用于防止未样式化内容(FOUC)的闪烁

The page you saw is probably using Modernizr . 您看到的页面可能正在使用Modernizr It is a JavaScript library that detects HTML5 and CSS3 features in the user's browser and works by adding a class "no-js" to the HTML element and when the page loads Modernizr replace it with set of rules that you're testing. 它是一个JavaScript库,可检测用户浏览器中的HTML5和CSS3功能,并通过向HTML元素添加“ no-js”类来工作,并且当页面加载Modernizr时,将其替换为要测试的规则集。

Check Modernizr website for more details if you want to test specific feature in a browser. 如果要在浏览器中测试特定功能,请访问Modernizr网站以获取更多详细信息。

Of ir you don't want to use Modernizr you can do it with JavaScript by placing this directly within your <title> tag : 在您不希望使用Modernizr的情况下,您可以将JavaScript直接放置在<title>标记中,以实现JavaScript:

<script>
    document.documentElement.className = document.documentElement.className.replace(/(\s|^)no-js(\s|$)/, '$1' + 'js' + '$2');
</script>

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

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