获取div标签的特殊属性的值 [英] Getting the value of a div tag's special attribute

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

问题描述

如果是下一个命令:

 console.log(document.getElementById('container'));

打印:

 <div id="container" prjid="ABCDE">...</div>

为什么下一个命令:

 console.log(document.getElementById('container').prjid);

打印未定义?我想获得prjid的价值

prints undefined? I am trying to get the value of prjid

推荐答案

prjid 是一个属性。您应该使用 getAttribute 获取任何属性值。

prjid is an attribute. You should use the function getAttribute to get any attributes value.


getAttribute()返回元素上指定属性的值。如果给定的属性不存在,则返回的值将为null或(空字符串);

getAttribute() returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string);







 console.log(document.getElementById('container').getAttribute("prjid"));

 <div id="container" prjid="ABCDE">...</div>

这篇关于获取div标签的特殊属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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