gpt4 book ai didi

jquery - 使用 jQuery 获取数据属性?

转载 作者:行者123 更新时间:2023-12-01 08:40:46 24 4
gpt4 key购买 nike

我试图从下面的链接获取data-shareurl以在jQuery中使用,但由于某种原因,当我将变量放入警报中时,它总是未定义。谁能告诉我我做错了什么?谢谢。我做了一个 fiddle here

HTML

<a title="" class="fancybox" data-shareurl="www.google.com" href="http://fancyapps.com/fancybox/demo/1_b.jpg">
<img src="http://fancyapps.com/fancybox/demo/1_b.jpg" title="" /></a>

jQuery

 var shareurl = $(this).attr('data-shareurl'); //Get this url
$(".fancybox").fancybox({

beforeShow: function() {
this.title = '<div class="addthis addthis_toolbox addthis_default_style "><a href="' + this.href + '" addthis:url="' + this.href + '" addthis:title="' + this.title + '" class="addthis_button_preferred_1"></a></div>';

alert(shareurl); //see the variable


},

afterShow: function() {
addthis.toolbox(
$(".addthis").get()
);
},
helpers: {
title: {
type: 'inside'
}
}

});

最佳答案

给你一个解决方案

$('a').click(function(){
console.log($(this).data('shareurl'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a title="" class="fancybox" data-shareurl="www.google.com" href="http://fancyapps.com/fancybox/demo/1_b.jpg">
<img src="http://fancyapps.com/fancybox/demo/1_b.jpg" title="" /></a>

希望这对您有帮助。

关于jquery - 使用 jQuery 获取数据属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47726952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com