admin管理员组文章数量:1291337
I have the following HTML below from Wikipedia main page at /. I'm trying to get the href text
//en.wikipedia/
<div class="central-featured-lang lang1" lang="en">
<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">
<strong>English</strong><br>
<em>The Free Encyclopedia</em><br>
<small>5 077 000+ articles</small>
</a>
</div>
I've tried this $$('.central-featured-lang.lang1 a[href$="/"]')
but I still get the whole output, not just the href text.
[<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">…</a><strong>English</strong><br><em>The Free Encyclopedia</em><br><small>5 077 000+ articles</small></a>]
Any advice is much appreciated.
I have the following HTML below from Wikipedia main page at https://www.wikipedia/. I'm trying to get the href text
//en.wikipedia/
<div class="central-featured-lang lang1" lang="en">
<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">
<strong>English</strong><br>
<em>The Free Encyclopedia</em><br>
<small>5 077 000+ articles</small>
</a>
</div>
I've tried this $$('.central-featured-lang.lang1 a[href$="/"]')
but I still get the whole output, not just the href text.
[<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">…</a><strong>English</strong><br><em>The Free Encyclopedia</em><br><small>5 077 000+ articles</small></a>]
Any advice is much appreciated.
Share Improve this question edited Apr 29, 2016 at 9:02 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked Apr 29, 2016 at 1:40 MingMing 3324 silver badges17 bronze badges3 Answers
Reset to default 3In Javascript you can use document.querySelector along with href
attribute, like this:
var url = document.querySelector('.central-featured-lang.lang1 a[href$="/"]').href;
alert(url);
<div class="central-featured-lang lang1" lang="en">
<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">
<strong>English</strong>
<br>
<em>The Free Encyclopedia</em>
<br>
<small>5 077 000+ articles</small>
</a>
</div>
Use the .attr()
Method:
$('.central-featured-lang.lang1 a[href$="/"]').attr("href")
var url = $('.central-featured-lang.lang1 a[href$="/"]').attr("href");
alert( url );
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="central-featured-lang lang1" lang="en">
<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">
<strong>English</strong><br>
<em>The Free Encyclopedia</em><br>
<small>5 077 000+ articles</small>
</a>
</div>
Use DOM Element getAttribute() Method
getAttribute() returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string); see Notes for details.
var element = document.querySelector('a.link-box'),
link = element.getAttribute('href');
alert(link)
<div class="central-featured-lang lang1" lang="en">
<a href="//en.wikipedia/" title="English — Wikipedia — The Free Encyclopedia" class="link-box">
<strong>English</strong>
<br>
<em>The Free Encyclopedia</em>
<br>
<small>5 077 000+ articles</small>
</a>
</div>
本文标签: javascriptHow to get href Text using CSS selectorsStack Overflow
版权声明:本文标题:javascript - How to get href Text using CSS selectors - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741508334a2382454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论