获取< a>的文字标签 [英] Getting the text of a <a> tag

查看:88
本文介绍了获取< a>的文字标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取我网页中所有超链接的文本,并检查文本是否已选中。如果它被选中,将会提示显示id的提示。



然而,当我尝试使用inputs [i] .value获取文本,我得到的结果是未定义。



我该如何去做这件事?

以下代码用于我的超链接:

 < a href =www.google.comid =1>已选择< ; / A> 
< a href =www.facebook.comid =2>选择< / a>
< a href =www.gmail.comid =3>已选择< / a>
< button onclick =check()>检查!< / button>

以下代码适用于我的javascript:

  function check(){
var inputs = document.getElementsByTagName('a');
for(var i = 0; i< inputs.length; i + = 1){
if(inputs [i] .value ==selected)
{
alert(输入[i] .id);




解决方案

使用 textContext 来代替。

  if(inputs [i ] .textContent ==selected)

http://jsfiddle.net/k6cpvt5e/2/


I am trying to get the text of all hyperlinks in my web page and check whether the text is "selected". If it is "selected", an alert showing the id will be prompted.

However, when I tried to use inputs[i].value to get the text of the , the result I gets was "undefined".

How can I go about doing it?

The following codes are for my hyperlinks:

<a href="www.google.com" id="1">selected</a>
<a href="www.facebook.com" id="2">select</a>
<a href="www.gmail.com" id="3">selected</a>
<button onclick="check()">Check!</button>

The following codes are for my javascript:

function check() {
    var inputs = document.getElementsByTagName('a');
    for(var i = 0; i < inputs.length; i += 1) {
        if (inputs[i].value == "selected")
        {
            alert(inputs[i].id);
        }
    }
}

解决方案

Use textContext instead.

if (inputs[i].textContent == "selected")

http://jsfiddle.net/k6cpvt5e/2/

这篇关于获取&lt; a&gt;的文字标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆