1

我有很多图像的视图

 <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');

}

它不起作用请帮助:

错误 this.getAttribute 不是函数

4

2 回答 2

2

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

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

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

var result = $(this).data('full-path');
于 2014-01-08T11:39:21.570 回答
1

使用.data('full-path')代替.data('img-path')

于 2014-01-08T11:39:33.587 回答