< noscript>的对面 [英] opposite of <noscript>

查看:74
本文介绍了< noscript>的对面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个与< noscript> 相反的HTML标签?也就是说,只有在启用了JavaScript的情况下才显示一些内容?例如:

Is there a HTML tag that does the opposite of <noscript>? That is, displays some content only if JavaScript is enabled? For example:

<ifscript>
<h1> Click on the big fancy Javascript widget below</h1>    
<ifscript>

当然< ifscript> doesn'实际上存在。我知道我可以通过使用JavaScript向DOM添加< h1> 来获得相同的结果,但如果可能的话,我更愿意使用(X)HTML来完成此操作。

Of course <ifscript> doesn't actually exist. I know I could achieve the same result by adding <h1> to the DOM using JavaScript, but if I'd prefer to do this with (X)HTML if possible.

谢谢,
Donal

Thanks, Donal

推荐答案

code>< script> 是for。只需要使用CSS隐藏特定的部分,然后使用JavaScript来显示它。以下是一个基本的启动示例:

There the <script> is for. Just initially hide the particular piece using CSS and use JavaScript to display it. Here's a basic kickoff example:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2297643</title>
        <script>
            window.onload = function() {
                document.getElementById("foo").style.display = 'block';
            };
        </script>
        <style>
            #foo { display: none; }
        </style>
    </head>
    <body>
        <noscript><p>JavaScript is disabled</noscript>
        <div id="foo"><p>JavaScript is enabled</div>
    </body>
</html>

...或者,在 jQuery ready()

...or, with little help of jQuery ready() which is a tad sooner with displaying the content:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2297643 with jQuery</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
            $(document).ready(function() {
                $('#foo').show();
            });
        </script>
        <style>
            #foo { display: none; }
        </style>
    </head>
    <body>
        <noscript><p>JavaScript is disabled</noscript>
        <div id="foo"><p>JavaScript is enabled</div>
    </body>
</html>

为改善用户体验,请考虑将< script> 在需要切换的特定HTML元素之后直接调用,以便不存在内容快闪或元素随机播放。 Andrew Moore在这个主题中给出了很好的例子

To improve user experience, consider placing the <script> call directly after the particular HTML element which needs to be toggled, so that there's no "flash of content" or "element shuffle". Andrew Moore has given a good example in this topic.

或者,您可以在< noscript>中使用< style> 来完成相反的操作(hacky) 。这在语法上是无效的,但IE6以后的所有浏览器都允许使用它,包括W3C严格的Opera:

Alternatively, you can do it (hacky) the other way round with <style> in <noscript>. This is syntactically invalid, but allowed by all browsers from IE6 and up, including the W3C-strict Opera:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2297643 with CSS in noscript</title>
    </head>
    <body>
        <noscript>
             <style>#foo { display: none; }</style>
             <p>JavaScript is disabled
        </noscript>
        <div id="foo"><p>JavaScript is enabled</div>
    </body>
</html>

这篇关于&lt; noscript&gt;的对面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆