我有一个包含大量图像的 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/