javascript - 用JS改变header下的url

标签 javascript jquery html

我正在尝试使用 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 秒,您将看到链接正在更新)

https://jsfiddle.net/nq8hwr2x/

关于javascript - 用JS改变header下的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46753787/

相关文章:

javascript - 不能使用 angular ajax,因为它显示 No 'Access-Control-Allow-Origin' 。但是可以使用 xmlhttprequest 和 jquery 3x 来做到这一点

javascript - 提取传单中标记的属性,onClick 事件

javascript - Javascript 将 json 文件中的数据存储到 map 中?

javascript - 无法在 jquery 中获取 PHP 值

javascript - 如何替换正确的开始和结束标签

javascript - 使数据属性表现得像 anchor

javascript - 通过谷歌地图上的经纬度数组获取距离

javascript - 如何在 lit-element 中实现 lit-element-bootstrap 包

javascript - json 对象 javascript 困惑

javascript - 如何在 jQuery 中控制后退和前进按钮