本文介绍了jQuery:访问数据链接属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从图像单击获取数据链接属性.
i want to get data-link attribute form an image click .
以下是我的图片标签
<a href="#" class="aclick">
<img data-link="petroliumjelly.php" src="productsimgs/petroliumjelly.png" class="img-responsive" id="2">
</a>
要点击它,这就是我正在做的,
to get it on click this is what i am doing ,
<script type="text/javascript" language="javascript">
$(".aclick").click(function (e) {
e.preventDefault();
alert($(this).attr("data-link"));
var link = $(this).data("data-link");// tried both
console.log(link);
});
</script>
当我尝试记录或提醒它时,我无法定义请帮我如何解决
when i try to log or alert it, i get undefined Please help me how can i fix it
推荐答案
下面是可帮助您的代码段.数据链接属性位于img标签中,而不位于"a"标签中.
Below is code snippet to help you. data-link attribute is in img tag and not in 'a' tag.
$(".aclick").click(function (e) {
e.preventDefault();
alert($(this).find('img').attr("data-link"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="aclick">
<img data-link="petroliumjelly.php" src="productsimgs/petroliumjelly.png" class="img-responsive" id="2">
</a>
这篇关于jQuery:访问数据链接属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!