繁体   English   中英

如何<a>使用javascript</a>更改<a>标记</a>的值<a>?</a>

[英]How to change the value of <a> tag using javascript?

这是我的HTML代码

<a href="myfile.html" id='tagId'>Old File</>

我想用名称“New File”更改标签的值

所以我写了像document.getElementById(“tagId”)这样的javascript。值='新文件';

我认为输出像<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说的那样,用得像

 document.getElementById("tagId").textContent="new File",

由于<a>标签没有value属性,因此需要更改锚标签的html。

使用Jquery

$("#tagId").html("new File");

要么

$("#tagId").text("new File");

编辑

如果你想使用javascript更改href,请像这样使用

 document.getElementById("tagId").href="new href";

使用jquery,

$("#tagid").attr("href","new value");

您可以使用.html()来获取/设置html:

 $('#tagId').html('New File')

要么

 $("#tagId").text("new File");

使用.textContent属性设置<a>的文本。

document.getElementById("tagId").textContent ="new File"

用这个:

$("#tagid").text("New File");

这是简单的代码

<script>
    $("tagId").html("new File");

</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM