如何在 JavaScript 中获取 <a> 标签的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51518727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:35:42  来源:igfitidea点击:

How to get value of <a> tag in JavaScript

javascripthtmlanchor

提问by TIto Amoo

I have this code:

我有这个代码:

<html>
<BUTTON ONCLICK="stups()">VALUE FINDER </BUTTON>
<a value="this is a value" href="value"><img src="b5.jpg"> values rule!!</a>
<p id="demo"></p>
<script>
var a = document.getElementsByTagName("a").value
function stups(){

document.getElementById("demo").innerHTML=a

}
</script>


</html>

The javascript is simply meant to get the value of th (link) when the button is clicked I don't even know what is supposed to be the value whether it is th hrefattribute, the valueattribute ,the value of the imgattribute or the text in between the two <a>and </a>,well I've tried EVERYTHING i could think of and it never gives me any value it keeps giving me the word undefined.Please help.

javascript 只是为了在单击按钮时获取 th(链接)的值我什至不知道应该是什么值,无论是 thhref属性、value属性、属性值img还是中的文本两者之间<a></a>,以及我已经试过所有我能想到的,它从来没有给我,它不断给我的字的任意值 undefined。请帮助。

NB:I needthat to be in between the <a></a>because it is a link

注意:我需要它介于两者之间,<a></a>因为它是一个链接

Thanks in advance

提前致谢

回答by BraveButter

You need to define which attribute you need from the atag.

您需要从a标签中定义您需要的属性。

So try .getAttribute('value')

所以试试 .getAttribute('value')

<html>
<button onclick="stups()">VALUE FINDER </button>
<a value="this is a value" href="value"><img src="b5.jpg"> values rule!!</a>
<p id="demo"></p>
<script>

function stups(){
   var a = document.getElementsByTagName('a')[0].getAttribute('value');
   document.getElementById("demo").innerHTML=a

}
</script>
</html>

回答by Takit Isy

First of all, take care of what you want to do, because .getElementsByTagName("a")will return you a collection of elements.

首先,照顾好你想要做什么,因为它.getElementsByTagName("a")会返回一个元素集合。

Then, you might want to use .getAttribute("value"):

然后,您可能想要使用.getAttribute("value")

var a = document.getElementsByTagName("a");

function stups(){
  document.getElementById("demo").innerHTML = a[0].getAttribute("value");
}
<BUTTON ONCLICK="stups()">VALUE FINDER </BUTTON>
<a value="this is a value" href="value"><img src="b5.jpg"> values rule!!</a>
<p id="demo"></p>



? ? ?

? ? ?

If you want to manage multiple aelements, you could do the following:

如果要管理多个a元素,可以执行以下操作:

  • Use a .querySelectorAll("a"), to be able to use a forEachloop directly,
  • .push()your values in an array,
  • Do what you want with your array.
  • 使用 a .querySelectorAll("a"), 可以forEach直接使用循环,
  • .push()您在数组中的值,
  • 用你的阵列做你想做的事。

var as = document.querySelectorAll("a");

function stups(){
  var values = [];
  as.forEach(function(a, index){
    values.push(a.getAttribute("value") || '--- no value ---');
    // OR: values.push(as[index].getAttribute("value"));
  })
  document.getElementById("demo").innerHTML = values.join('<br>');
}
<BUTTON ONCLICK="stups()">VALUE FINDER </BUTTON>
<a value="this is a value" href="value"><img src="b5.jpg"> values rule!!</a>
<a value="this is another value" href="value"><img src="b5.jpg"> values rule!!</a>
<a href="value"><img src="b5.jpg">No value here</a>
<a value="this is another value, again" href="value"><img src="b5.jpg"> values rule!!</a>
<p id="demo"></p>

Hope it helps.

希望能帮助到你。

回答by Aplet123

In these situations, MDN is your friend.You also shouldn't be copy-pasting code you don't understand. The srcof the imgpoints to whatever image is being displayed. The hrefof the atag is the actual link. The text inside the a tag is what shows up. You should not be setting the valueattribute of the atag as it is non-standard and not needed. document.getElementsByTagName("a")returns an array of every atag in the document. You need to specify the first link by running document.getElementsByTagName("a")[0]. You can get the link just by using .href. In the end, the stupsfunction should look something like this:

在这些情况下,MDN 是您的朋友。您也不应该复制粘贴您不理解的代码。在src对的img点,正在显示任何图像。在href该的a标签是实际的链接。a 标签内的文本是显示的内容。您不应该设置标签的value属性,a因为它是非标准的且不需要。document.getElementsByTagName("a")返回a文档中每个标签的数组。您需要通过运行指定第一个链接document.getElementsByTagName("a")[0]。您只需使用 即可获取链接.href。最后,该stups函数应如下所示:

function stups(){
    var a = document.getElementsByTagName("a")[0].href;
    document.getElementById("demo").innerHTML = a;
}