我是javascript的noob,這可能是一個愚蠢的問題,但我寫了一個簡單的javascript函數來滑動到網頁上的特定元素。 代碼如下:http://jsfiddle.net/bhbTr/在html中放置和調用javascript函數
但是,當我嘗試將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>
我想不出我做錯了什麼,但我懷疑它是與沒有被鏈接調用的函數。我真的很感激,如果任何人有任何建議