如何强制<noscript>内联并继承CSS样式?</noscript>

时间:2010-03-04 05:54:09

标签: html css stylesheet inline noscript

在另一个标记内部使用<noscript>似乎会使其采用自己的样式(即没有),并强制内部文本采用自己的行(即使设置了display:inline用CSS)。有没有办法避免这种情况,或者使用替代方法?

这就是我的意思:http://www.webdevout.net/test?01I

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
  <head>
    <title>Test</title>
 <style type="text/css">
  p { font-family:sans-serif; font-size:12px;}
  noscript {display:inline !important;}
 </style>
  </head>
  <body>
    <p>This is some text<noscript> that should be displayed on the same line with the same style if JS if disabled.</noscript></p>
  </body>
</html>

4 个答案:

答案 0 :(得分:1)

我建议使用与脚本/ noscript标签不同的方法。

这样的效果最好:

<!DOCTYPE html>
<html class="noJS">
    <head>
    <title>noJS Demo</title>

    <style type="text/css">
        html.noJS .jsRequired,
        html .noJS{
            display: none !important;
        }

            html.noJS span.noJS{
                display: inline !important;
            }

            html.noJS div.noJS{
                display: block !important;
            }
    </style>

    <script type="text/javascript">
        function onDocumentLoad(){
            var html = document.getElementsByTagName("html")[0];
            html.className = html.className.replace("noJS", "");
        }
    </script>
    </head>
    <body onLoad="onDocumentLoad();">
        <p>This is some text<span class='noJS'> that should be displayed on the same line with the same style if JS if disabled.</span></p>
    </body>
</html>

当然,如果你使用像jQuery这样的框架,JavaScript就更容易了,只需从正文中删除JS函数和函数,然后只使用以下JS:

$(document).ready(function(){
    $("html").removeClass("noJS");
});

答案 1 :(得分:1)

旧的但仍然相关的问题 - http://www.tigerheron.com/article/2007/10/alternative-noscript-tag提供了一个非常简单的解决方案:

“将以下代码插入网页的<head>部分:

<script type="text/javascript"> document.write('<style>.noscript { display:none }</style>'); </script>

当您需要使用<noscript>内联时,请改用<span class="noscript">。“

答案 2 :(得分:0)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
  <head>
    <title>Test</title>
 <style type="text/css">
  p { font-family:sans-serif; font-size:12px;}
 </style>
  </head>
  <body>
    <script type="text/javascript">document.write('<p>This is some text</p>');</script>
    <noscript><p>This is some text that should be displayed on the same line with the same style if JS if disabled.</p></noscript>
  </body>
</html>

这样的事情应该做你想要的。你当然应该使用不引人注目的方法,但我想现在已经超过了它。

答案 3 :(得分:0)

您是否尝试过将类似span的元素放在noscript标记内,然后设置样式?这是一个很长的镜头,但可能有用。

或者,使用你的选择器非常具体,并给出一个镜头。 #content p noscript { display:inline !important; } 之类的内容可能有效。但它也可能是不可解决的。

作为最后的手段,你可以放弃noscript标签并放入一个span(或你选择的元素)并给它一个noscript类 - 然后删除你js中的第一个东西。