1
<html>

    <head>

        <script type="text/javascript">
         // jquery and javascript functions
        </script>

    </head>

    <body>

        <fancy-jquery-ajaxy-html-section>
        </fancy-jquery-ajaxy-html-section>

        <noscript>
        sorry you came to the wrong place - this site is all jquery/ajaxy stuff.
        </noscript>

    </body>

</html>

I tried surrounding <fancy-jquery-ajaxy-html> with a <script type="text/javascript"></script> but then nothing from that section is displayed even for users with javascript enabled.

But what I want to do is hide that <fancy-jquery-ajax-html> section only if the user doesn't have javascript enabled.

It contains content that is useless to someone without javascript turned on, so it shouldn't be shown at all.

A user with javascript disabled should only see a message saying that the page can't be viewed without javascript.

Is there a way do that?

4

5 回答 5

4

The easiest way is to hide the section with CSS (e.g. display:none), then show it through Javascript.

EDIT: just a little example

<div>Everyone sees this div</div>

<div id="mydiv" class="hidden">You see this div only with JS enabled</div>

<script type="text/javascript">
    $("#mydiv").removeClass("hidden");
</script>
<noscript>
    <div>You will see this div only with JS disabled</div>
</noscript> 

And, of course, in your CSS:

.hidden
  {
  display: none;
  }
于 2010-11-20T19:47:18.657 回答
2

您可以使用 css 隐藏您喜欢的部分:

<div id="fancy_jquery_ajax" style="display: none;">
</div>

那么你可以使用 JavaScript 来显示元素:

$("#fancy_jquery_ajax").css("display", "block");

我希望这是对的,我实际上并没有那么多使用 jQuery。:S

于 2010-11-20T19:54:39.327 回答
1

另一种方法是使用 JavaScript 生成该 HTML,因此除非 JavaScript 正在运行,否则它不会出现。

于 2010-11-20T19:57:51.787 回答
0

我所做的是让我的 javascript 隐藏nojsdiv并显示maindiv. 这样,如果您没有 javascript,则会显示该消息。

<body onload="allowlogin()">
    <div id="maindiv" style="visibility: hidden;">
...
    </div>
    <div id="nojsdiv">
        The training system requires javascript to be enabled.
    </div>
</body>
于 2010-11-20T19:49:09.263 回答
0

.js我更喜欢在jQuery 加载后立即向 html 标签添加一个类。这允许我编写仅在用户启用/禁用 javascript 时适用的 css 规则。这使您的显示和隐藏代码远离我们的 JS,并让 CSS 完成其工作并设置页面样式

以下是我将如何解决您的问题:

<html>
  <head>
    <style type="text/css">
      .js #fancy_jquery_ajax {display: none;}
    </style>
    <script type="text/javascript" src="/scripts/jquery.js"></script>
    <script type="text/javascript">
      $('html').addClass('js');
      $(document).ready(function() {
        // Stuff to do as soon as the DOM is ready
      });  
    </script>
  </head>
  <body>
      <div id = "fancy_jquery_ajax"></div>
      <noscript><!-- stuff to say if use had javascript disabled --></noscript>
  </body>
</html>

重要的是要注意,我们希望在jQuery 加载后立即.js添加类,而不是将其添加到我们的处理程序中。否则我们将回到第一方。document.ready

于 2010-11-20T20:45:38.580 回答