我正在尝试使用 js 函数动态更改标题下的 url。不幸的是我堆叠了一些简单的部分。
根据link我试图解决这个问题...
我的代码的简化版本:
<div class="details">
<h1 id="title">Old title</h1>
<h3 id="author">
<a id="aId" href="https://www.google.pl/">Old author</a>
</h3>
</div>
脚本:
var title = $("#title");// the id of the heading
var author = $("#author");// id of author
title.text("New title");
author.text("New author");
var a = document.getElementById('aId');
a.href = "bing.com"
问题是作者现在不可点击。你可以帮帮我吗?
编辑:
感谢您的帮助! 解决办法:
var title = $("#title");// the id of the heading
var author = $("#author a");// id of author
title.text("New title");
author.text("New author");
author.attr("href", "http://bing.com")
最佳答案
当您调用author.text("New author");
时,您将删除 <h3 id="author">
中的链接表单元素。它基本上来自:
<h3 id="author">
<a id="aId" href="https://www.google.pl/">Old author</a>
</h3>
至
<h3 id="author">New author</h3>
您应该更改链接中作者的姓名,而不是 h3
直接,如下:
var title = $("#title");// the id of the heading
var author = $("#author a");// id of author
title.text("New title");
author.text("New author");
var a = document.getElementById('aId');
a.href = "http://bing.com"
特别注意作者的选择器已经变成了a
#author
内的元素元素。
您可以在此处测试代码:(等待 3 秒,您将看到链接正在更新)
关于javascript - 用JS改变header下的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46753787/