如何使用JavaScript读取HTML标签的属性? [英] How do I read an attribute of a HTML tag using JavaScript?
本文介绍了如何使用JavaScript读取HTML标签的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个< span>
元素,我用JavaScript阅读。元素如下:
I have a <span>
element that I am reading with JavaScript. The element is like the following one:
<span key="789">
<input id="ctl00_cphBody_gvContact_ctl09_cbox" type="checkbox" name="ctl00$cphBody$gvContact$ctl09$cbox">
</span>
我需要获取key属性的值。如果我尝试获取HTML元素的内部HTML,它将返回内部复选框的HTML。
I need to get the value for the "key" attribute. If I try to get the inner HTML of the HTML element, it returns the HTML of the inner checkbox.
推荐答案
使用jQuery:
$('span').attr('key').
使用纯JavaScript
Using pure javascript
document.getElementsByTagName('span')[0].getAttribute('key')
这篇关于如何使用JavaScript读取HTML标签的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文