我正在创建一个网站,我想在点击某些部分时更改页面内容。我有CSS,HTML,JS是单独的文件。我是个菜鸟。使用jquery当我给标签(" *")选择所有元素时,我会收到警报,但对于我的生活,我无法选择特定的div。所有div都是唯一的但我真的不知道为什么查询不能选择特定的div。
$('*').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>
<script type="text/javascript" src="resume.js">
</script>
<body>
<div id="container">
<div id="topbar">
<div id="logo">
</div>
<div id="contact">
<div id="facebook">
<a href="https://www.facebook.com/dipranjan.chatterjee"></a></div>
<div id="mail">
<a href="mailTo:dipranjan.chatterjee@gmail.com"></a></div>
<div id="blog">
<a href="http://chotochotodukhoktha.blogspot.in/"></a></div>
</div>
<div id="name"> Dip Ranjan Chatterjee</div>
</div>
<div id="bottom">
<div id="sidebar">
<div id="about">About</div>
<div id="border"></div>
<div id="edu">Education</div>
<div id="border2"></div>
<div id="prof">Professional Exp.</div>
<div id="border3"></div>
<div id="portfolio">Portfolio</div>
</div>
<div id="contents"><p>
<p>Feel free to drop me a mail or message, if you need to get in touch with me.</p>
</div>
</div>
</body>
&#13;
另请注意,我有CSS html和JS都在单独的文件中。在JS文件中我放了jquery。
答案 0 :(得分:1)
确保您没有多个具有相同ID的div(在这种情况下为#topbar),并且您在加载文档后添加了侦听器。
$(document).ready(function(){
$('#topbar').click(function(){
alert("The paragraph was clicked.");
});
});
答案 1 :(得分:0)
显示所有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 -->