2017-12-09 2 views
1

Ich versuche, die Daten-shareurl aus dem folgenden Link für die Verwendung in jQuery zu bekommen, aber aus irgendeinem Grund, wenn ich die Variable in eine Warnung setzen, ist es immer undefiniert. Kann mir jemand sagen, was ich falsch gemacht habe? Vielen Dank. Ich habe eine Geige gemacht hereEin Datenattribut mit jQuery abrufen?

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' 
    } 
    } 

}); 

Antwort

1

Du this als Selektor. Ich nehme an, dass dieser Code außerhalb der Funktion ist $('.fancybox')...

Warum nicht die id oder class als Selektor verwenden.

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

Hier gehen Sie mit einer Lösung

$('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>

Hope this Ihnen helfen.

Verwandte Themen