如何获得链接 [英] how do get the link
问题描述
<h4 class="itemtitle"><a href="http://feedproxy.google.com/~r/rotofeed/~3/1K7ZKSC9I8k/">Avisail Garcia & Dee Gordon: Waiver Wire</a></h4>
i有以上html我需要获取链接和使用javascript的innertext任何人都可以帮助我。
i have above html i need get the link and innertext with javascript can anybody help me.
推荐答案
最简单的方法是给a
标记一个id
:
The easiest way to do this, is to give thea
tag anid
:
<h4 class="itemtitle"><a href="http://feedproxy.google.com/~r/rotofeed/~3/1K7ZKSC9I8k/" id="titlelink">Avisail Garcia & Dee Gordon: Waiver Wire</a></h4>
请注意,此ID必须是唯一的。
然后,您可以使用此JavaScript获取内容:
Note that this ID must be unique.
Then, you can get the content using this JavaScript:
var linkContent = document.getElementById("titlelink").textContent;
你必须使用 textContent
,因为Firefox不支持 innerText
,但所有浏览器(IE 8及更早版本除外) )支持 textContent
。
如果你不能改变 a
标签,试试这个:
You have to use textContent
because Firefox doesn't support innerText
, but all browsers (except IE 8 and earlier) support textContent
.
If you can't change the a
tag, try this:
var linkContent = document.getElementsByClassName('itemtitle')[0].children[0].textContent;
上面代码中的第一个 0
表示你应该在第一个元素中获得 a
-tag的内容 itemtitle
class。例如,如果它是第二个,则将第一个 0
更改为 1
。请注意,IE 8及更早版本不支持 getElementsByClassName
。如果还应支持IE 8,请使用 getElementsByTagName
并传递'h4'
作为参数。
The first 0
in the above code indicates that you should get the content of the a
-tag in the first element with the itemtitle
class. If it's the second one, for example, then change the first 0
into a 1
. Note that IE 8 and earlier doesn't support getElementsByClassName
. If IE 8 should also be supported, use getElementsByTagName
and pass 'h4'
as parameter.
这篇关于如何获得链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!