gpt4 book ai didi

javascript - .load() 不适用于 ul li click

转载 作者:行者123 更新时间:2023-12-02 14:10:35 25 4
gpt4 key购买 nike

这是我的代码

$(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 中闪烁,有时会停留在其中,但如果我在按钮上运行相同的脚本,则单击它会毫无问题地加载页面。我怎样才能让它与 li 一起工作?

最佳答案

添加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");
});
});

关于javascript - .load() 不适用于 ul li click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39637853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com