我是 javascript 的菜鸟,这可能是一个愚蠢的问题,但我编写了一个简单的 javascript 函数来滑动到网页上的特定元素。这是代码:http: //jsfiddle.net/bhbTr/
但是,当我尝试将 javascript 和 html 放在一个 html 文件中时,它似乎不起作用。这是我所拥有的:
<head>
<title>Test</title>
<link rel='stylesheet' id='css' href='style1.css' type='text/css' media='all'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">>
function goToByScroll(id){
// Remove "link" from the ID
id = id.replace("link", "");
var off = $("#"+id).offset().top - 50;
// Scroll
$('html,body').animate({
scrollTop: off},
'slow');
}
$("#navbar > ul > li > a").click(function(e) {
// Prevent a page reload when a link is pressed
e.preventDefault();
// Call the scroll function
goToByScroll($(this).attr("id"));
});
$("#navbar > a").click(function(e) {
// Prevent a page reload when a link is pressed
e.preventDefault();
// Call the scroll function
goToByScroll($(this).attr("id"));
});
</script>
</head>
<body>
<!-- Begin Wrapper -->
<div id="wrapper">
<div class="navbar" id="navbar">
<a href="#" id="WhoWeArelink" class="navbarlogo">Test</a>
<ul>
<li><a id = "WhoWeArelink" href="#">Who We Are</a></li>
<li><a id = "WhatWeDolink" href="#">What We do</a></li>
<li><a id = "OurWorklink" href="#">Our Work</a></li>
<li><a id = "Contactlink" href="#">Contact Us</a></li>
</ul>
</div>
<div class="contentcontainer">
<div class="WhoWeAre" id="WhoWeAre">
<p class="header">uck</p>
<p class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="WhatWeDo"id="WhatWeDo">
<p class = "header">Projects</p>
<p class="info">Projects</p>
</div>
<div class="OurWork" id="OurWork">
<p class = "header">Resume</p>
<p class="info">Resume</p>
</div>
<div class="Contact" id="Contact">
<p class = "header">Contact</p>
<p class="info">Contact</p>
</div>
</div>
</div>
<!-- End Wrapper -->
<!-- Begin Footer -->
<div class="footer">
<p style="text-align:center;">Some bullshit copyright (c) insert year here</p>
</div>
<!-- End Footer -->
</body>
我不知道我做错了什么,但我怀疑这与链接没有调用的函数有关。如果有人有任何建议,我将不胜感激