简体   繁体   English

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

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

Here my html code 这是我的HTML代码

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

I want to change the value of tag with name "New File" 我想用名称“New File”更改标签的值

So i wrote javascript like document.getElementById("tagId").value='New File'; 所以我写了像document.getElementById(“tagId”)这样的javascript。值='新文件';

I thought output like <a href="myfile.html" id='tagId'>New File</a> 我认为输出像<a href="myfile.html" id='tagId'>New File</a>

But it's not working .can anyone help ? 但它不起作用。任何人都可以帮忙吗?

Use .innerHTML or .innerText 使用.innerHTML.innerText

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

OR 要么

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

OR Note: Not all browsers support innerTEXT , so use textContent if you want to change the text only, 或注意:并非所有浏览器都支持innerTEXT ,因此如果只想更改文本,请使用textContent

Reference Stack overflow answer 参考堆栈溢出答案

So like @Amith Joki said, use like 就像@Amith Joki说的那样,用得像

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

Since <a> tag doesn't have value property, you need change the html of anchor tags. 由于<a>标签没有value属性,因此需要更改锚标签的html。

Using Jquery 使用Jquery

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

OR 要么

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

Edit 编辑

If you want to change the href using javascript, USe like this 如果你想使用javascript更改href,请像这样使用

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

USing jquery, 使用jquery,

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

You can use .html() to get/set html: 您可以使用.html()来获取/设置html:

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

OR 要么

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

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

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

用这个:

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

Here is simple code 这是简单的代码

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

</script>

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

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