1

Using <noscript> inside of another tag seems to cause it to take on its own style (that is, none), and forces the text inside to take its own line (even if display:inline is set with CSS). Is there any way to avoid this, or a workaround to use instead?

Here is what I mean: 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

4 回答 4

1

我会推荐一种与使用 script/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 就更简单了,只需从 body 中删除 JS 函数和函数,然后使用以下 JS:

$(document).ready(function(){
    $("html").removeClass("noJS");
});
于 2011-05-18T02:11:20.327 回答
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>inline 时,请<span class="noscript">改用。”

于 2012-08-23T14:49:33.773 回答
0

您是否尝试过在 noscript 标签中放置一个像 span 这样的元素,然后设置 span 的样式?这是一个很长的镜头,但可能会奏效。

或者,使用您的选择器非常具体并试一试。类似的东西#content p noscript { display:inline !important; } 可能会起作用。但它也可能无法解决。

作为最后的手段,您可以放弃 noscript 标记并放入一个跨度(或您选择的元素)并给它一个 noscript 类 - 然后在您的 js 中删除第一件事。

于 2010-03-04T09:48:06.853 回答
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>

像这样的东西应该做你想做的事。你当然应该使用不显眼的方法,但我想这暂时高于标准。

于 2010-03-04T08:37:46.613 回答