0

在我的 html 代码中,我有这个

<li class="register"><a href="">Registreer</a></li>

<div id="content">

</div>

我尝试使用头部中的以下代码将 html 文件加载到 div

<script type="text/javascript" src="includes/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="includes/js/navbar.js"></script>

导航栏.js:

$(document).ready(function() {
$(function(){
  $(".register").click(function(){
    $("#content").load("/signup.html");
    document.write("test");
  });
});
});

我已经在我的网页文件夹中复制了 signup.html。但它不起作用。但是它确实显示了 1/4 秒的测试。我也尝试将我的 js 代码直接放在 html 文件中,但这也不起作用。

4

3 回答 3

1

你在重复自己:

$(document).ready(function() {
   $(function(){ //same as $(document).ready(function() {
     $(".register").click(function(){
       $("#content").load("/signup.html");
       document.write("test");
     });
   });
});

尝试这样

   $(function(){ 
     $(".register").click(function(){
       $("#content").load("/signup.html");
       document.write("test");
     });
   });

此外,您可能想尝试停止链接的默认事件:

   $(function(){ 
     $(".register").click(function(){
       $("#content").load("/signup.html");
       document.write("test");
     });         
     $(".register").on('click', 'a', function(e){
        e.preventDefault(); //prevents action of the link
     });
   });
于 2013-03-11T15:58:58.593 回答
1
$(document).ready(function () {
    $(".register").click(function(){
        $("#content").load("/signup.html");
        return false;
     });
});

通过从 click 事件返回 false ,超链接元素将知道不执行其默认操作(这就是页面刷新的原因)。

于 2013-03-11T16:02:40.493 回答
0

When using a relative URL, the URL is relative to the page on which the JavaScript is run. In your case, since you are using the string '/signup.html', It will look for the singup.html file up one directory from the directory of the page currently being viewed.

Use the F12 Development Console in IE or Chrome, FireBug, of Fiddler to view your AJAX requests and results to see whether your 'singup.html' is being loading from the appropriate directory. You can also view source after the load completes to see if there is HTML beiing loaded into your DIV.

于 2013-03-11T16:03:00.673 回答