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&nbsp;077&nbsp;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&nbsp;077&nbsp;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&nbsp;077&nbsp;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&nbsp;077&nbsp;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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

In 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&nbsp;077&nbsp;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&nbsp;077&nbsp;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&nbsp;077&nbsp;000+ articles</small>
  </a>
</div>

本文标签: javascriptHow to get href Text using CSS selectorsStack Overflow