2016-09-22 110 views
0

这里是我的代码.load()不工作UL李点击

$(document).ready(function(){ 
 
    $("#work").click(function(){ 
 
    $("#container").load("about/work.html"); 
 
    }); 
 
    $("#profile").click(function(){ 
 
    $("#container").load("profile.html"); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul> 
 
    <li><a id="work" href="">work</a></li> 
 
    <li><a id="profile" href="">profile</a></li> 
 
</ul> 
 

 
<div id="container"></div>

当我点击工作或轮廓请求的页面只是闪烁在div容器和某个停留在但若我运行同样的脚本按钮点击它加载页面没有任何问题。我如何使它与李一起工作?

+0

你检查你的浏览器控制台上的任何错误的值#。 ? –

+0

您的点击处理程序不会停止浏览器执行链接的默认行为:转到href属性中定义的位置(空值=这个非常相同的位置) - > ['.preventDefault()']( https://api.jquery.com/event.preventdefault/) – Andreas

+0

不要将'href'作为空字符串。给它一个值:'href =“#”' –

回答

1

添加event.preventDefault

$(document).ready(function(){ 
    $("#work").click(function(event){ 
    event.preventDefault(); //changed here 
    $("#container").load("about/work.html"); 
    }); 
    $("#profile").click(function(event){ 
    event.preventDefault(); //changed here 
    $("#container").load("profile.html"); 
    }); 
}); 
+0

它的工作!谢谢 –

0

尝试onclick

$(document).ready(function() { 
    $("#work").on("click", function() { 
     $("#container").load("about/work.html"); 
    }); 
}); 
1

有两件事情在这里改变。

1)尽量不要将您的href值留空。如果你这样做,那么浏览器将重定向到同一页面。给它如果可能的话href="#"

2)有preventDefault()功能从<a>单击事件失踪

$(document).ready(function(){ 
    $("#work").click(function(e){ 
     e.preventDefault(); 
     $("#container").load("about/work.html"); 
    }); 
    $("#profile").click(function(e){ 
     e.preventDefault(); 
     $("#container").load("profile.html"); 
    }); 
});