如何强制

发布于 2024-08-23 08:22:30 字数 832 浏览 7 评论 0原文

在另一个标签内使用 似乎会导致它采用自己的样式(即无),并强制内部文本采用自己的行(即使 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>

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>

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

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

发布评论

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

评论(4

七堇年 2024-08-30 08:22:30

我会推荐使用 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 就更容易了,只需删除 JS 函数和函数体中的函数,然后只需使用以下 JS:

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

I would recommend a different approach over using script/noscript tags.

Something like this works best:

<!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>

Of course, if you use a framework like jQuery the JavaScript is even easier, just remove the JS function and the function from the body, then just use the following JS:

$(document).ready(function(){
    $("html").removeClass("noJS");
});
看透却不说透 2024-08-30 08:22:30

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

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

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

当您需要使用 内联,请改用 。”

Old but still relevant question - http://www.tigerheron.com/article/2007/10/alternative-noscript-tag presents an excellent and very simple solution:

"Insert the following code into the <head> section of your Web page:

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

When you need to use <noscript> inline, use <span class="noscript"> instead."

2024-08-30 08:22:30
<!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>

像这样的事情应该做你想做的事。您当然应该使用不引人注目的方法,但我想目前这已经高于标准了。

<!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>

Something like this should do what you want. You should of course use unobtrusive methods instead but I guess that´s above par for now.

请止步禁区 2024-08-30 08:22:30

您是否尝试过将像 span 这样的元素放入 noscript 标记中,然后设置 Span 的样式?这是一个很遥远的事情,但可能会奏效。

或者,您可以非常具体地选择您的选择器并尝试一下。类似于 #content p noscript { display:inline !important; } 可能有效。但它也可能是无法解决的。

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

Have you tried putting an element like a span inside the noscript tag, and then styling the span? It's a long shot, but might work.

Alternatively, get very specific with your selector and give that a shot. Something like #content p noscript { display:inline !important; } might work. But it might also be insoluble.

As a last resort, you could ditch the noscript tag and put in a span (or your element of choice) and give it a class of noscript -- then remove that first thing in your js.

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