如何使用JavaScript读取href的内容? [英] How to read the contents of a href with javascript?

查看:85
本文介绍了如何使用JavaScript读取href的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html,并且对如何读取href标记的内容感到困惑?

I have the following html and am stumped as to how to read the contents of the href tag?

<p class="xyz-title">
    <a href="http://www.youtube.com/embed/xyz">
        <span class="field-content">Title here</span>
    </a>
</p>

我尝试了document.getElementByClass('xyz-title')[0].innerHTML,但是显然没有用.

I tried document.getElementByClass('xyz-title')[0].innerHTML but that didn't work obviously.

非常感谢您向我指出了正确的方向.

Thanks very much for pointing me in the right direction.

推荐答案

这是语法错误.它应该是 getElementsByClassName .改用它:

It is a syntax error. It is supposed to be getElementsByClassName. Use this instead:

document.getElementsByClassName('xyz-title')[0].innerHTML

要在<p class="xyz-title">中选择<a>标签,您需要使用以下代码:

And for selecting the <a> tag inside the <p class="xyz-title"> You need to use this code:

document.getElementsByClassName('xyz-title')[0].children[0].href
document.getElementsByClassName('xyz-title')[0].getElementsByTagName('a')[0].href

或者,您可以简单地使用:

Or, you can simply use:

document.getElementsByTagName('a')[0].href

这篇关于如何使用JavaScript读取href的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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