<div class="page">
<a href="another-page.php">Click!</a>
</div>
<script>
$("a").click(function(){
$(".page").load($(this).attr("href"));
window.history.pushState("", "", '/'+$(this).attr("href"));
return false;
});
</script>
<!-- in another-page.php -->
<a href="initial-page.php">Click here!</a>
<!-- what the another-page.php looks like once it's been loaded by jquery -->
<a href>Click here!</a>
然后单击链接将加载 <div class="page">
中的页面但是,我注意到在页面顶部有一个 <h1><a href="/">Title</a></h1>
它失去了我定义的颜色 h1 a {...}
然后在检查元素时,它不是上面的 h1,而是 <h1><a href>Title</a></h1>
最佳答案
这里还有其他问题。
当您使用 <a href=/>
时,会出现我描述的问题不是<a href="/">
。我没有测试检查使用 <a href=another-page.php>
时是否会发生同样的情况。但发现引用正斜杠解决了问题。
关于javascript - jquery load() 导致标签丢失其 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41908222/