javascript - jquery load() 导致标签丢失其 href 值

标签 javascript jquery html

<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/

相关文章:

javascript - 向原型(prototype)添加方法

javascript - 模拟javascript从下拉列表中选择

html - 在 HTML 中链接 CSS 文件

javascript - 如果我用 javascript 隐藏文本输入元素,其他 javascript 函数是否仍然可以访问输入到其中的值?

javascript - 如何使用 should.js 进行 deepEqual 断言?

jquery - 如何向 jQuery Mobile 中的元素添加填充

php - 回合制游戏得分记录

javascript - 悬停时替换为图像

html - 适合页面问题

javascript - 全日历同步