开发者

How do I read an attribute of a HTML tag using JavaScript?

开发者 https://www.devze.com 2023-03-26 02:37 出处:网络
I have a <span> element that I am reading with JavaScript. The element is like the following one:

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>

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.


Using jQuery:

$('span').attr('key').

Using pure javascript

document.getElementsByTagName('span')[0].getAttribute('key')


JQuery makes it really easy. See the .attr method

You'll probably want to assign an id or class to that span so that you can select it specifically:

<span id="idOfSpan" key="blah">...</span>

var spanKey = $('#idOfSpan').attr('key')
0

精彩评论

暂无评论...
验证码 换一张
取 消