代码之家  ›  专栏  ›  技术社区  ›  Re0sless

是否有与<noscript>相反的HTML?

  •  88
  • Re0sless  · 技术社区  · 16 年前

    HTML中是否有一个标记,仅在启用javascript时才显示其内容?我知道 <noscript> 相反,在关闭javascript时显示其HTML内容。但我只想在javascript可用的情况下在站点上显示一个表单,告诉他们如果没有该表单,他们为什么不能使用它。

    我知道怎么做的唯一方法就是 document.write(); 方法中的一个脚本标记,对于大量的HTML来说似乎有点混乱。

    11 回复  |  直到 7 年前
        1
  •  49
  •   ceejayoz    16 年前

        2
  •  184
  •   Will    16 年前

    <html>
    <head>
        <noscript><style> .jsonly { display: none } </style></noscript>
    </head>
    
    <body>
        <p class="jsonly">You are a JavaScript User!</p>
    </body>
    </html>
    

        3
  •  8
  •   Chris Bloom    14 年前

    <html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
         $(document).ready(function() {
           $.get('_test.html', function(html) {
             $('p:first').after(html);
           });
         });
        </script>
    </head>
    <body>
      <p>This is content at the top of the page.</p>
      <p>This is content at the bottom of the page.</p>
    </body>
    </html>
    

    <p>This is from an HTML fragment document</p>
    

    <p>This is content at the top of the page.</p>
    <p>This is from an HTML fragment document</p>
    <p>This is content at the bottom of the page.</p>
    
        4
  •  8
  •   RedClover Josh Stodola    7 年前

    $(document).ready(function() {
        $("body").addClass("js");
    });
    

    no-js

    $(document).ready(function() {
        $("body").removeClass("no-js");
        $("body").addClass("js");
    });
    

        5
  •  3
  •   Spycho    14 年前

    <html>
        <head>
            <!-- put this in a separate stylesheet -->
            <style type="text/css">
                .jsOff .jsOnly{
                    display:none;
                }
            </style>
        </head>
    
        <body class="jsOff">
            <script type="text/javascript">
                document.body.className = document.body.className.replace('jsOff','jsOn');
            </script>
    
            <noscript><p>Please enable JavaScript and then refresh the page.</p></noscript>
    
            <p class="jsOnly">I am only shown if JS is enabled</p>
        </body>
    </html>
    

    .jsOn .someClass{} .jsOff .someOtherClass{} <noscript>

    .jsOn .noJsOnly{
        display:none;
    }
    
        6
  •  1
  •   TheSmurf    16 年前

        7
  •  1
  •   Shadow2531    16 年前

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <style>
                *[data-when-js-is-on] {
                    display: none;
                }
            </style>
            <script>
                document.getElementsByTagName("style")[0].textContent = "";
            </script>
        </head>
        <body>
            <div data-when-js-is-on>
                JS is on.
            </div>
        </body>
    </html>
    

        8
  •  1
  •   prgDevelop    11 年前

    .js {
    display: none;
    }
    

    $(document).ready(function() {
        $(".js").css('display', 'inline');
        $(".no-js").css('display', 'none');
    });
    

    <span class="js">Javascript is enabled</span>
    <span class="no-js">Javascript is disabled</span>
    
        9
  •  0
  •   Ross    16 年前
        10
  •  0
  •   dkeeney    16 年前

    <

        11
  •  0
  •   cpm    16 年前

    document.getElementById(id).innerHTML = "My Content"