如何读取HTML标签中的href

.innerHTML will give you the content of the element, not the value of any attributes. .href will give you the href value.

To select the anchor element, try:

1
document.getElementsByClassName('xyz-title')[0].getElementsByTagName('a')[0].href

Or, simpler:

1
document.querySelector('.xyz-title a').href

Example:

1
var x=document.getElementsByClassName("gallery-item");for(i=0;i<x.length;i++)console.log(x[i].getElementsByTagName('div')[0].getElementsByTagName('a')[0].href);

Reference

  1. How to read the contents of a href with javascript?