我正在尝试获取放置在html页面上的特定锚标签的href值。
html标签结构
<a rel="nofollow" title="some title" href="http://www.this-is-what-i-need.com/just-this.html"><span><em class="buttonGo"></em>Go to this page</span></a>
如何使用buttonGo类名从锚标记上方获取href值?
一行简单的Javascript将为您提供所需的内容:
var href = document.getElementsByClassName("buttonGo")[0].parentNode.parentNode.href;
但是,如果不确定要使用多少次parentNode
,则必须使用循环:
var anchor = document.getElementsByClassName("buttonGo")[0];
do{
anchor = anchor.parentNode;
} while(anchor.nodeName.toLowerCase() != "a");
var href = anchor.href;
演示:解决方案一(由Milche Patern创建)和解决方案二
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句