修改脚本以检查数据相关

Modify script to check for a data-rel

本文关键字:数据 检查 脚本 修改      更新时间:2023-09-26

我正在尝试修改此脚本以检查锚标记是否不包含 prettyPhoto[product-gallery] 的数据rel .

我曾几次尝试在第 3 行添加||,但没有运气。任何帮助将不胜感激。

$('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"]').each(function(){
  //single image popup
    if ($(this).parents('.iwmp-gallery').length == 0) { //Line #3
      $(this).magnificPopup({
        type:'image',
          callbacks: {
            open: function() {
             $('.mfp-description').append(this.currItem.el.attr('alt'));
},
                          afterChange: function() {
                            $('.mfp-description').empty().append(this.currItem.el.attr('alt'));
                          }
                        },
                            image: {
                                markup: 
                                '<div class="mfp-figure">'+
                                '<div class="mfp-close"></div>'+
                                '<div class="mfp-img"></div>'+
                                '<div class="mfp-bottom-bar">'+
                                '<div class="mfp-title"></div>'+
                                '<div class="mfp-description"></div>'+
                                '<div class="mfp-counter"></div>'+
                                '</div>'+
                                '</div>',
                                titleSrc: function(item) {
                                    return item.el.find('img').attr('alt');
                                }                           
                            }
                        });
                    }
                });

谢谢

您可以修改选择器

$('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"]') 

对于data-rel

$('a[href*=".jpg"][data-rel], a[href*=".jpeg"][data-rel], a[href*=".png"][data-rel], a[href*=".gif"][data-rel]')

或者,如果您需要 or 条件,您可以修改if 条件为 :

if (($(this).parents('.iwmp-gallery').length == 0)||$(this).attr("data-rel"))

参考 : 具有属性

对于非数据发布:

$('a[href*=".jpg"]:not([data-rel]), a[href*=".jpeg"]:not([data-rel]), a[href*=".png"]:not([data-rel]), a[href*=".gif"]:not([data-rel]'))

和 for 或条件:

if (($(this).parents('.iwmp-gallery').length == 0)||(!$(this).attr("data-rel").length))

答案是

if (($(this).parents('.iwmp-gallery').length == 0)||(!$(this).attr("data-rel").length))

感谢@KK