如何从没有ID的锚点标签中提取href值

huangapple go评论66阅读模式
英文:

How to extract href value from anchor tag with no ID

问题

如何从没有id的锚点标签中获取href值的最佳方法。它看起来像这样:

<a href="www.google.com">mydoc.pdf</a>

通常我会使用getElementById,但在这种情况下无法使用。我还尝试过getElementsByTagName,但由于页面上有很多这样的标签,这几乎不可能。

英文:

What is the best way to grab the href value from the anchor tag with the anchor tag has no id. It looks like this:

&lt;a href=&quot;www.google.com&quot;&gt;mydoc.pdf&lt;/a&gt;

Usually I would use getElementById, but can't in this case. I also tried getElementsByTagName, but since there are so many on the page it would be impossible.

答案1

得分: 1

在这种情况下,您的标签没有唯一的值可供查询,如id、name等等。

如果您知道它的索引或"mydoc.pdf",您可以获取它。

示例:

var atags = document.getElementByTagName("a");
var selectedByIndex = atags[index];
var selectedbyText = atags.filter(function(tag,index) {
return tag.innerHTML === "mydoc.pdf";
});
英文:

In this case, your tag does not have a unique value to query like id, name, ...

If you know it's index or "mydoc.pdf" you can get it.

Sample:

var atags = document.getElementByTagName(&quot;a&quot;);
var selectedByIndex = atags[index];
var selectedbyText = atags.filter(function(tag,index) {
return tag.innerHTML === &quot;mydoc.pdf&quot;;
});

huangapple
  • 本文由 发表于 2023年6月15日 11:24:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定