-1

我正在自学Javascript(使用Jquery),我只是想在点击功能上进行显示/隐藏。

现在,我正在使用 Sublime Text 2 执行此操作,并尝试通过本地主机在浏览器中查看它——我也在使用 HTML5 Boilerplate 执行此操作。

下面是我的代码。

调用 Jquery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

脚本

 <script type="text/javascript">
    $(document).ready(function()
{
    $('p').hide();
});

  </script>

HTML

<a href="#" id="paragraphAnchor">click me</a>
 <p>This is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a test</p>

我正在我的 HTML 文档中完成所有这些工作。不是最佳实践,但我认为我会从小处着手。

现在我这样做了,文本甚至没有被隐藏。

为什么是这样?

更新:

<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <title></title>
  <meta name="description" content="">
  <meta name="author" content="">

  <meta name="viewport" content="width=device-width,initial-scale=1">

  <!-- CSS concatenated and minified via ant build script-->
  <link rel="stylesheet" href="css/style.css">
  <!-- end CSS-->

  <script src="js/libs/modernizr-2.0.6.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function()
{
  $('p').hide();
  $(a#paragraphAnchor).click(function()
  {
    $('p').show();
  });
});

  </script>
</head>

<body>


 <a href="#" id="paragraphAnchor">click me</a>
 <p>This is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a test</p>


  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>


  <!-- scripts concatenated and minified via ant build script-->
  <script defer src="js/plugins.js"></script>
  <script defer src="js/script.js"></script>
  <!-- end scripts-->


  <script> // Change UA-XXXXX-X to be your site's ID
    window._gaq = [['_setAccount','UAXXXXXXXX1'],['_trackPageview'],['_trackPageLoadTime']];
    Modernizr.load({
      load: ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js'
    });
  </script>


  <!--[if lt IE 7 ]>
    <script src="//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js"></script>
    <script>window.attachEvent('onload',function(){CFInstall.check({mode:'overlay'})})</script>
  <![endif]-->

</body>
</html>

更新:问题已解决

看来 HTML 5 Boilerplate 应该使用

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>

代替:

<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

或者至少我是这样让它工作的。

​</p>

4

5 回答 5

1

尝试这个:

<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <title></title>
  <meta name="description" content="">
  <meta name="author" content="">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <!-- CSS concatenated and minified via ant build script-->
  <link rel="stylesheet" href="css/style.css">
  <!-- end CSS-->

  <script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script type='text/javascript' src="js/libs/modernizr-2.0.6.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
       $('p').hide();
       $('#paragraphAnchor').click(function() {
           $('p').show();
       });
    });
  </script>
</head>

jQuery 未加载到您的页面中,请注意您应该为选择器使用引号。

于 2012-08-18T14:42:41.430 回答
1

jQuery URL 缺少协议。这一行:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

应该读

http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js _

编辑:这是因为文件是在本地加载的,所以假定的协议file将无法通过 HTTP(S) 加载任何 jquery 库。

这可以通过检查控制台来确认。铬报告:

Failed to load resource file://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
于 2012-08-18T14:25:16.347 回答
0

您似乎在加载 jquery 代码之前调用它。

添加这个:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>

在这之前:

<script type="text/javascript">
    $(document).ready(function()
{
  $('p').hide();
  $('a#paragraphAnchor').click(function()
  {
    $('p').show();
  });
});

  </script>

例如:

<head>
  ...

  <script src="js/libs/modernizr-2.0.6.min.js"></script>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>

  <script type="text/javascript">
    $(document).ready(function()
{
  $('p').hide();
  $('a#paragraphAnchor').click(function()
  {
    $('p').show();
  });
});

  </script>
</head>
...
于 2012-08-18T14:41:02.383 回答
0

检查此代码它工作正常

<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
 <script>
    $(document).ready(function()
{
      $("p").hide();
      $("a").click(function () {
      $("p").slideToggle("slow");
    });
});
</script>

</head>
<body>
<a href="#" id="paragraphAnchor">click me</a>
 <p>This is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a testThis is  a test</p>
 </body>
</html>
于 2012-08-18T14:37:52.467 回答
0

这是您在$(document).ready函数中需要的

$('#paragraphAnchor').click(function() {
  $('p').toggle();
});
于 2012-08-18T14:24:45.383 回答