这是我的html代码
<a href="myfile.html" id='tagId'>Old File</>
我想更改名为“新文件”的标签的值
所以我写了 javascript,比如 document.getElementById("tagId").value='New File';
我认为输出像 <a href="myfile.html" id='tagId'>New File</a>
但它不起作用。有人可以帮忙吗?
最佳答案
使用 .innerHTML
或 .innerText
document.getElementById("tagId").innerHTML="new File",
或
document.getElementById("tagId").innerText="new File",
或
注意:并非所有浏览器都支持 innerTEXT
, 所以使用 textContent
如果您只想更改文本,
就像@Amith Joki 说的那样,使用 like
document.getElementById("tagId").textContent="new File",
自 <a>
标签没有 value 属性,您需要更改 anchor 标签的 html。
使用 Jquery
$("#tagId").html("new File");
或
$("#tagId").text("new File");
编辑
如果你想使用 javascript 更改 href,请像这样使用
document.getElementById("tagId").href="new href";
使用 jquery,
$("#tagid").attr("href","new value");
关于javascript - 如何使用 javascript 更改 <a> 标签的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934978/