英文:
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:
<a href="www.google.com">mydoc.pdf</a>
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("a");
var selectedByIndex = atags[index];
var selectedbyText = atags.filter(function(tag,index) {
return tag.innerHTML === "mydoc.pdf";
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论