如何使用 jquery 获取 data-src 属性?
How to get data-src attribute with jquery?
实际上我想做的是当我悬停时第一张图片必须再次出现
我想和你分享我的结构
<div class="tur-list-box">
<div class="tur-list-content">
<figure>
<img data-src="img/assets/tourlist-2.jpg" class="lazy" src="img/assets/placeholder.png" alt="tur sayfası">
<a href="#" class="figure-overlay">
<p class="tour-price">
<span class="price-big">73,</span>
<span class="price-little">40</span>
<span class="price-unit">TL*</span>
<span class="price-block">‘den itibaren</span>
</p>
</a>
</figure><!-- tur resim-->
<div class="tur-details">
<h3><a href="#">Hafta Sonu Turları</a></h3>
<p>15 farklı program</p>
<i class=" open-tur-toggle fa fa-chevron-down" aria-hidden="true"></i>
</div><!-- tur detay-->
</div><!-- tur list content-->
<div class="tur-list-toggle">
<ul class="list-unstyled">
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/orta/2016-02/otel_buyuk-abant-oteli_vPYKBnet58y0itPrkpce.jpg">Kakava ( Hıdırellez ) Şenlikleri Alaçatı <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/orta/2016-10/otel_abant-palace-hotel_FTfyg8HYVB9lNeOUMA76.jpg">Ot Festivali Urla Enginar Festivali Turu <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-01/tur_adana-portakal-cicegi-karnavali_3eO46CjOg4k34ooQM2mA.jpg">Adana Portakal Çiçeği Karnavalı Isparta <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-01/tur_isparta-goller-yoresi-gul-hasadi-turu_Ue7lCTZhtuNk6DHTOy5C.jpg">Gül Hasadı Ve Göller Yöresi Turları <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-03/tur_manisa-mesir-macunu-senligi-turu_ElEY2IdzFOfHLe6do7ja.jpg">Manisa Mesir Macunu Şenliği Turu <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-01/tur_isparta-goller-yoresi-gul-hasadi-turu_KN8aDpGyF4O6gKABF5d4.jpg">Uçaklı Festival Turları <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
</ul>
</div><!-- acilir kapanir alan-->
</div><!-- tur list box-->
<script>
$(".tur-list-box").hover(
function(){
$(this).find(".tur-list-toggle").stop().slideDown();
$(this).find(".open-tur-toggle").stop().removeClass("fa-chevron-down").addClass("fa-chevron-up");
},
function(){
var getDefaultImg = $(this).find("figure img").data(".lazy");
console.log(getDefaultImg);
$(this).find(".tur-list-toggle").stop().slideUp();
$(this).find(".open-tur-toggle").stop().removeClass("fa-chevron-up").addClass("fa-chevron-down");
}
);
$('.tur-list-toggle ul li a').hover(
function(e) {
e.preventDefault();
var getAttr = $(this).attr("data-img");
$(this).parents(".tur-list-box").find("figure img").attr("src",getAttr);
},
function(e) {
}
);
</script>
and I want to share with you demo link to see how to work
顺便说一下,如果您在检查元素上看不到 data-src
,请尝试查看源代码(ctrl+U 表示 chrome)
$(this).attr("data-src")
您可以查看此 link 了解更多信息 How to get the data-id attribute?
您可以尝试使用以下行:
var getDefaultImg = $(this).find("figure img").data("src");
更多关于数据函数的信息:https://api.jquery.com/data/
$(selector).data("src")
将获取 data-src 属性的值
您实际上想要获取 src
而不是 data-src
属性,以便在悬停后找到图像的来源,正如您的代码所示,所以它是 var getDefaultImg = $(this).find("figure img").attr("src");
。
那是因为您使用的延迟加载图像插件在图像加载完成时将 data-src
替换为 src
。
例如(取自plugin's demo)
<!--image 6 load is complete-->
<img class="lazy img-responsive" src="images/6.jpg?t=1477297898" style="display: block;">
<!--image 7 not loaded yet-->
<img class="lazy img-responsive" data-src="images/7.jpg?t=1477297898" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">
实际上我想做的是当我悬停时第一张图片必须再次出现 我想和你分享我的结构
<div class="tur-list-box">
<div class="tur-list-content">
<figure>
<img data-src="img/assets/tourlist-2.jpg" class="lazy" src="img/assets/placeholder.png" alt="tur sayfası">
<a href="#" class="figure-overlay">
<p class="tour-price">
<span class="price-big">73,</span>
<span class="price-little">40</span>
<span class="price-unit">TL*</span>
<span class="price-block">‘den itibaren</span>
</p>
</a>
</figure><!-- tur resim-->
<div class="tur-details">
<h3><a href="#">Hafta Sonu Turları</a></h3>
<p>15 farklı program</p>
<i class=" open-tur-toggle fa fa-chevron-down" aria-hidden="true"></i>
</div><!-- tur detay-->
</div><!-- tur list content-->
<div class="tur-list-toggle">
<ul class="list-unstyled">
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/orta/2016-02/otel_buyuk-abant-oteli_vPYKBnet58y0itPrkpce.jpg">Kakava ( Hıdırellez ) Şenlikleri Alaçatı <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/orta/2016-10/otel_abant-palace-hotel_FTfyg8HYVB9lNeOUMA76.jpg">Ot Festivali Urla Enginar Festivali Turu <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-01/tur_adana-portakal-cicegi-karnavali_3eO46CjOg4k34ooQM2mA.jpg">Adana Portakal Çiçeği Karnavalı Isparta <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-01/tur_isparta-goller-yoresi-gul-hasadi-turu_Ue7lCTZhtuNk6DHTOy5C.jpg">Gül Hasadı Ve Göller Yöresi Turları <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-03/tur_manisa-mesir-macunu-senligi-turu_ElEY2IdzFOfHLe6do7ja.jpg">Manisa Mesir Macunu Şenliği Turu <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
<li><a href="#" data-img="http://cdn.anitur.com.tr/resimler/normal/2016-01/tur_isparta-goller-yoresi-gul-hasadi-turu_KN8aDpGyF4O6gKABF5d4.jpg">Uçaklı Festival Turları <i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
</ul>
</div><!-- acilir kapanir alan-->
</div><!-- tur list box-->
<script>
$(".tur-list-box").hover(
function(){
$(this).find(".tur-list-toggle").stop().slideDown();
$(this).find(".open-tur-toggle").stop().removeClass("fa-chevron-down").addClass("fa-chevron-up");
},
function(){
var getDefaultImg = $(this).find("figure img").data(".lazy");
console.log(getDefaultImg);
$(this).find(".tur-list-toggle").stop().slideUp();
$(this).find(".open-tur-toggle").stop().removeClass("fa-chevron-up").addClass("fa-chevron-down");
}
);
$('.tur-list-toggle ul li a').hover(
function(e) {
e.preventDefault();
var getAttr = $(this).attr("data-img");
$(this).parents(".tur-list-box").find("figure img").attr("src",getAttr);
},
function(e) {
}
);
</script>
and I want to share with you demo link to see how to work
顺便说一下,如果您在检查元素上看不到 data-src
,请尝试查看源代码(ctrl+U 表示 chrome)
$(this).attr("data-src")
您可以查看此 link 了解更多信息 How to get the data-id attribute?
您可以尝试使用以下行:
var getDefaultImg = $(this).find("figure img").data("src");
更多关于数据函数的信息:https://api.jquery.com/data/
$(selector).data("src")
将获取 data-src 属性的值
您实际上想要获取 src
而不是 data-src
属性,以便在悬停后找到图像的来源,正如您的代码所示,所以它是 var getDefaultImg = $(this).find("figure img").attr("src");
。
那是因为您使用的延迟加载图像插件在图像加载完成时将 data-src
替换为 src
。
例如(取自plugin's demo)
<!--image 6 load is complete-->
<img class="lazy img-responsive" src="images/6.jpg?t=1477297898" style="display: block;">
<!--image 7 not loaded yet-->
<img class="lazy img-responsive" data-src="images/7.jpg?t=1477297898" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">