jquery data属性获取该元素

标签 jquery this jquery-data

我有一个包含大量图像的 View

 <img src="@Path" data-img-prev="@UrlPreview" data-full-path="@UrlImage" onclick=Get()  />

它像这样以 html 形式呈现

<img img-prev="1.jpg" data-full-path="https://...1.jpg" src="https://...1.jpg">
<img img-prev="2.jpg" data-full-path="https://...2.jpg" src="https://...1.jpg"> 
<img img-prev="3.jpg" data-full-path="https://...3.jpg" src="https://...1.jpg"> 

我希望函数从每个元素获取数据

function Get() {
this.getAttribute('img-path');
this.data('img-path');

}

它不起作用,请帮忙:

error this.getAttribute is not a function

最佳答案

数据属性称为data-full-path,而不是img-path

var result = this.getAttribute('data-full-path');

或者因为您使用 jQuery 标记了它(假设您位于点击处理程序上下文中):

var result = $(this).data('full-path');

关于jquery data属性获取该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994577/

相关文章:

c++ - Lambda`s internal this in c++

javascript - 检索多个数据属性并设置为子元素的文本

javascript - jQuery .data 方法 - 根据存储数据动态添加或删除元素

javascript - 如何验证具有多个选项卡的表单?

php - ajax 出现问题。这应该可行吗?

javascript - 使用 jQuery 从通过 FileReader 上传的 html 文件中提取表格

javascript - 了解涉及 `this` 的 `arguments` 行为

javascript - 为什么在执行谷歌地图方向服务回调时DomWindow对象是 'this'

javascript - jQuery 如果集合中的任何项目的 data-id 等于 foo

jquery - 从iframe内的元素遍历到父iframe TAG