2015-02-08 72 views
0

我正在制作一个网站,我想在点击某些部分时更改页面的内容。我有CSS,HTML,JS是分离文件。我是一个noob。使用jquery当我给标签(“*”)选择所有元素时,我收到一条警告,但对于我的生活,我无法选择特定的div。所有的div都是独一无二的,但我真的不知道为什么查询不能选择特定的div。specfic元素不能用jquery选择

$('*').click(function() 
 
    {  alert("The paragraph was clicked."); 
 
    });

---->工作

但这不是

$('#topbar').click(function(){ 
 
     alert("The paragraph was clicked."); 
 
    });

这里是整个代码:

<!doctype html> 
 
<html> 
 
<head> 
 
<title>dear C</title> 
 
<meta charset="utf-8" /> 
 
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
 
<meta name="viewport" content="width=device-width, initial-scale=1" /> 
 
<link rel="stylesheet" type="text/css" href="bbc.css"> 
 
</head> 
 
<link href='http://fonts.googleapis.com/css?family=Slabo+27px' rel='stylesheet' type='text/css'> 
 
<link href='http://fonts.googleapis.com/css?family=Libre+Baskerville:400italic' rel='stylesheet' type='text/css'> 
 
<link href='http://fonts.googleapis.com/css?family=Quicksand' rel='stylesheet' type='text/css'> 
 
<script type="text/javascript" src="jquery-2.1.3.min.js"> 
 
</script> \t 
 
<script type="text/javascript" src="resume.js"> 
 
</script> \t 
 
<body> 
 

 
<div id="container"> 
 
    \t <div id="topbar"> 
 
\t \t <div id="logo"> 
 
\t \t </div> 
 
\t \t <div id="contact"> 
 
\t \t \t \t <div id="facebook"> 
 
\t \t \t \t <a href="https://www.facebook.com/dipranjan.chatterjee"></a></div> 
 
\t \t \t \t <div id="mail"> 
 
\t \t \t \t <a href="mailTo:[email protected]"></a></div> 
 
\t \t \t \t <div id="blog"> 
 
\t \t \t \t <a href="http://chotochotodukhoktha.blogspot.in/"></a></div> 
 
\t \t </div> 
 
\t \t <div id="name"> Dip Ranjan Chatterjee</div> 
 
\t \t </div> 
 
\t \t <div id="bottom"> 
 
\t \t <div id="sidebar"> 
 
\t \t \t <div id="about">About</div> 
 
\t \t \t <div id="border"></div> 
 
\t \t \t <div id="edu">Education</div> 
 
\t \t \t <div id="border2"></div> 
 
\t \t \t <div id="prof">Professional Exp.</div> 
 
\t \t \t <div id="border3"></div> 
 
\t \t \t <div id="portfolio">Portfolio</div> 
 
\t \t </div> 
 
\t \t <div id="contents"><p> 
 

 
<p>Feel free to drop me a mail or message, if you need to get in touch with me.</p> 
 
\t \t </div> 
 
\t </div> 
 

 
</body>

还要注意我有CSS HTML和JS都在单独的文件。在JS文件中,我把jquery。

+3

你能粘贴html吗? – beck03076 2015-02-08 15:49:10

+0

你看过:http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – 2015-02-08 15:50:33

+0

只要检查是否有一个具有该ID的div,并且在文档准备好之后添加了监听器 – 2015-02-08 15:52:08

回答

1

确保您没有多个具有相同ID的div(在这种情况下为#topbar),并且在文档加载后添加侦听器。

$(document).ready(function(){ 
    $('#topbar').click(function(){ 
     alert("The paragraph was clicked."); 
    }); 
}); 
+0

非常感谢..此工作。我是jquery的新手,你可以让我知道,ready函数的specilaity是什么? – 2015-02-09 03:02:55

+0

@DipRanjanChatterjee请阅读文档:[链接](http://api.jquery.com/ready/)基本上在页面“准备就绪”之前,元素不会加载到DOM中,#topbar不存在然而。 – Tom 2015-02-09 08:26:22

0

显示你所有的HTML代码

  1. 标识符ID = “顶栏” 存在吗?
  2. id =“topbar”在HTML中是唯一的吗?
  3. 你正在加载HTML后声明脚本?
<div id="topbar">Go</div> 
<div id="topbar2">Go2</div> 

<script type="text/javascript"> 
$('#topbar').click(function(){ 
    alert("The paragraph was clicked."); 
}); 
</script> 

<!-- works --> 
+0

这是整个代码: – 2015-02-09 02:55:16

+0

感谢您的帮助 – 2015-02-09 03:03:12