如何更改< a>的值标签使用JavaScript? [英] How to change the value of <a> tag using javascript?

查看:126
本文介绍了如何更改< a>的值标签使用JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我的html代码

 < a href =myfile.htmlid ='tagId'>旧文件< ; /> 

我想更改名称为New File的标签的值。 b

所以我写了javascript,像document.getElementById(tagId)。value ='New File';



c $ c>< a href =myfile.htmlid ='tagId'>新文件< / a>



但它不工作。任何人都可以帮忙?

使用 .innerHTML .innerText

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

OR

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



OR
注意:并非所有浏览器都支持 innerTEXT ,所以如果您想要使用 textContent 只改变文字,



参考堆栈溢出swer



就像@Amith Joki说的那样,使用像

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

code>< a> 标签没有value属性,您需要更改锚标记的html。



使用Jquery

  $(#tagId)。html(new File); 

OR

  $(#tagId)。text(new File); 

编辑

如果你想用javascript来改变href,USe就像这样

  document.getElementById(tagId)。href = 新的href; 

使用jquery,

  $(#tagid)。attr(href,new value); 


Here my html code

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

I want to change the value of tag with name "New File"

So i wrote javascript like document.getElementById("tagId").value='New File';

I thought output like <a href="myfile.html" id='tagId'>New File</a>

But it's not working .can anyone help ?

解决方案

Use .innerHTML or .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,

Reference Stack overflow answer

So like @Amith Joki said, use like

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

Since <a> tag doesn't have value property, you need change the html of anchor tags.

Using Jquery

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

OR

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

Edit

If you want to change the href using javascript, USe like this

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

USing jquery,

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

这篇关于如何更改&lt; a&gt;的值标签使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆