TypeError: abc.getAttribute不是函数

对于下一个代码:


<span class="map-marker" data-lng="101.7113506794"></span>
<span class="map-marker" data-lng="101.6311097146"></span>

var abc = $/'.map-marker:first'/;
var xyz = abc.getAttribute/"data-lat"/;
console.log/xyz/;


我收到一条错误消息:
TypeError: abc.getAttribute is not a function

. 我做错了什么?
已邀请:

詹大官人

赞同来自:

尝试它可能是:


var abc = $/".map-marker:first"/[0];
var xyz = abc.getAttribute/"data-lat"/;
console.log/xyz/;


或者那个:


var abc = $/".map-marker:first"/;
var xyz = abc.data/"lat"/;
console.log/xyz/;

董宝中

赞同来自:

abc

- 这个对象 jQuery, 所以他没有功能
getAttribute//

. 它有一个功能
attr//

.

帅驴

赞同来自:

我做错了什么?

你看了

一个东西 jQuery

作为

元素

DOM . 对象 jQuery 没有方法
getAttribute

. 相反,你可以使用
http://api.jquery.com/attr/
或者
http://api.jquery.com/data/
.

董宝中

赞同来自:

你选择几个项目。 功能 select 返回一个数组,数组没有函数 getAttribute. 您可以使用 for loop 用于迭代选择和接收属性值,或者您可以使用索引器 /例如, [0]/, 获取特定对象的属性。

冰洋

赞同来自:

你正试图得到 "data-lat", 但你只有 "data-lng" 定义。

二哥

赞同来自:

观看访问
this

在具有动态可见区域的事件的处理程序中。

呼叫函数
this

, 哪些不存在可能导致
node.getAttribute is not a function

.

裸奔

赞同来自:

刚刚进入这个问题。 同意 @Felix kling.
尝试以下:


<span class="map-marker" data-lng="101.7113506794"></span>
<span class="map-marker" data-lng="101.6311097146"></span>
<script>
var abc = $/'.map-marker:first'/;
var xyz = $/abc/.attr/"data-lng"/;
console.log/xyz/;
</script>

要回复问题请先登录注册