如何显示一个
如果启用 Javascript 并且使用不同的
如果不是?

发布于 2024-07-23 07:25:02 字数 1246 浏览 1 评论 0原文

我正在使用一些 Javascript 在

中显示“照片淡入淡出旋转器”。 但是,如果未启用 JS,我会显示一个 gif 动画。 我实际上让它与这段代码一起工作:

<div id="slideshow" class="show pics float-left">
    <img src="images/banner-photos/new-01.png" width="343" height="228" alt="photo" />
    <img src="images/banner-photos/new-02.png" width="343" height="228" alt="photo" />
    <img src="images/banner-photos/new-03.png" width="343" height="228" alt="photo" />
</div>

<noscript>
    <link href="css/hide-slideshow.css" rel="stylesheet" type="text/css" />
    <p class="adjust"><img src="images/banner-photos/animation.gif" alt="slideshow" /></p>
</noscript>

外部 JS 文件在

上发挥了作用。 id="slideshow" 上的类设置尺寸、填充、边距、定位等。“hide-slideshow”css 文件在 #slideshow 上执行“display:none”,动画 gif 周围的“调整”类执行一些操作推动和拉动以将其放置在需要的位置(如果幻灯片未隐藏,这就是幻灯片所在的位置)。

我已经在 FF3、IE7、IE8、Chrome 和 Safari (Win) 中对此进行了测试。 好消息是它就像一个魅力。 坏消息是它没有通过 W3C 验证器。 我收到一条错误消息:“文档类型不允许此处的元素“链接””。

虽然我的方法有效,但是是不是太笨拙了? 有没有更好的方法来显示一个

如果启用了 JS,如果没有,则显示不同的
,以一种跨浏览器工作的方式通过验证?

谢谢!

I'm using some Javascript to display a "photo fade rotator" in a <div>. If JS is not enabled, however, I've got an animated gif I want to display instead. I actually have it working with this code:

<div id="slideshow" class="show pics float-left">
    <img src="images/banner-photos/new-01.png" width="343" height="228" alt="photo" />
    <img src="images/banner-photos/new-02.png" width="343" height="228" alt="photo" />
    <img src="images/banner-photos/new-03.png" width="343" height="228" alt="photo" />
</div>

<noscript>
    <link href="css/hide-slideshow.css" rel="stylesheet" type="text/css" />
    <p class="adjust"><img src="images/banner-photos/animation.gif" alt="slideshow" /></p>
</noscript>

External JS files do their magic on the <div>. The classes on id="slideshow" set dimensions, padding, margin, positioning, etc. The "hide-slideshow" css file does a "display:none" on #slideshow, and the "adjust" class around the animated gif does some pushing and pulling to get it where it needs to be (which is where the slideshow would be if it wasn't hidden).

I've tested this in FF3, IE7, IE8, Chrome, and Safari (Win). The good news is that it works like a charm. The bad news is that it doesn't pass the W3C validator. I get an error that says, "document type does not allow element "link" here".

Even though my method works, is it too kludgey? Is there a better way to show one <div> if JS is enabled and a different <div> if it's not, in a way that works cross-browser and passes the validation?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新雨望断虹 2024-07-30 07:25:02

尝试这样的事情。 它会优雅地降级,并且不需要 标记。

<div id="hasJavaScript" style="display: none">
    <!-- Contents hidden when JavaScript is disabled -->
</div>

<div id="noscript">
    <!-- Contents shown only when JavaScript is disabled -->
</div>

<script type="text/javascript">

    document.getElementById('hasJavaScript').style.display = 'block';
    document.getElementById('noscript').style.display = 'none';

</script>

为了使示例简短,我使用了 ID。 使用类会更合适。 此外,最不错的入门框架,例如 HTML5 BoilerplateModernizr 将使用 HTML 标签上的 CSS 类来执行此操作,因此您可以通过全局方式隐藏/显示 CSS 文件中的内容,而不是使用 JS。 标签以 no-js CSS 类开头,并被 JS 中的 js 类替换。 这允许您只为依赖于 JS 存在的任何 CSS 规则添加适当的类前缀。 这更具语义性,因为它将表现性事物(应该在视觉上隐藏/显示的事物)与逻辑事物(无论页面是否运行 JS)分开。


回答原始问题的另一个选择是默认在 head 中添加指向 CSS 文档的链接,然后使用 JavaScript 将其删除。 (免责声明)我还没有对此进行测试,但它也应该适用于所有浏览器。

<script type="text/javascript">
    var cssDocs = document.getElementsByTagName('LINK');
    for (var i = 0; i < cssDocs.length; i++) {
        var css = cssDocs[i];
        if (css.href === 'css/hide-slideshow.css') {
            var parent = css.parentNode;
            parent.removeChild(css);
            break;
        }
    }
</script>

如果您使用 jQuery,此脚本也可以缩短。

$("link[href='css/hide-slideshow.css']").remove();

Try something like this. It degrades gracefully and doesn't require a <noscript /> tag.

<div id="hasJavaScript" style="display: none">
    <!-- Contents hidden when JavaScript is disabled -->
</div>

<div id="noscript">
    <!-- Contents shown only when JavaScript is disabled -->
</div>

<script type="text/javascript">

    document.getElementById('hasJavaScript').style.display = 'block';
    document.getElementById('noscript').style.display = 'none';

</script>

To keep the example short, I used IDs. It would be more appropriate to use classes. Also, most decent starter frameworks such as HTML5 Boilerplate and Modernizr will do this using CSS classes on the HTML tag so you have a global way to hide/show content from your CSS file instead of using JS. The <html /> tag starts with a no-js CSS class and it gets replaced by a js class in JS. This allows you to just prefix any CSS rules that are dependent on the existence of JS with the appropriate class. This is more semantic since it separates presentational things (what should be visually hidden/shown) from logical things (whether the page is running JS or not).


Another option for answering the original question is to add the link to the CSS document in the head by default and remove it using JavaScript. (DISCLAIMER) I haven't tested this, but it should also work across all browsers.

<script type="text/javascript">
    var cssDocs = document.getElementsByTagName('LINK');
    for (var i = 0; i < cssDocs.length; i++) {
        var css = cssDocs[i];
        if (css.href === 'css/hide-slideshow.css') {
            var parent = css.parentNode;
            parent.removeChild(css);
            break;
        }
    }
</script>

This script could also be shortened if you're using jQuery.

$("link[href='css/hide-slideshow.css']").remove();
烟酉 2024-07-30 07:25:02

只需为非 Javascript div 提供其自己的类,并将其样式定义放入样式表中即可。 将其包含在 块中。 您可以使用脚本动态插入另一个 div

Just give the no-Javascript div its own class and put its style definition in your stylesheet. Include it in a <noscript> block. You can dynamically insert the other div with your script.

小梨窩很甜 2024-07-30 07:25:02

我的 元素属于 因此出现错误。

简单的解决方案是将“no javascript”css 放入主 css 文件中,该文件在 中加载

I've <link...> element belongs in the <head...> hence the error.

The simple solution is to place the "no javascript" css into a main css file that is loaded in the <head...>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文