0

任何人都可以帮助我,我有画廊,每个 li 都有数据标题,我想将该值添加到 url,我尝试没有运气它总是说未定义,但我有帖子 ID,我很容易将它添加到 url。任何人都知道如何将该 data-title 属性传递给 url。

    function getPhoto(postId, hasSelector) {

    //first visible value
    var title = jQuery(this).children("li").attr("data-title");
    var next = jQuery('#gallery-wrap li.visible').first().attr('id').split('photo-')[1];

    var next_title = jQuery('#gallery-wrap li.visible').first().attr('data-title')[1];

    //last visible value
    var prev = jQuery('#gallery-wrap li.visible').last().attr('id').split('photo-')[1];

    var prev_title = jQuery('#gallery-wrap li.visible').last().attr('data-title')[1];

    //has next var
    var hasNext = jQuery(hasSelector).parent().next();

    //has prev var
    var hasPrev = jQuery(hasSelector).parent().prev();


    //if there is a next object
    if(hasNext.length != 0) {

        while(hasNext.hasClass('visible') == false && hasNext.length != 0) {
            hasNext = hasNext.next();
        }

        if(hasNext.length != 0) {
            var next = hasNext.attr('id').split('photo-')[1];
            var next_title = hasNext.attr('data-title')[1];
        }
    }

    //if there is a previous object
    if(hasPrev.length != 0) {

        while(hasPrev.hasClass('visible') == false && hasPrev.length != 0) {
            hasPrev = hasPrev.prev();
        }

        if(hasPrev.length != 0) {
            var prev = hasPrev.attr('id').split('photo-')[1];
            var prev_title = hasPrev.attr('data-title')[1];
        }
    }


    // prev ids and post ids of photo   
    var _this = jQuery('#photo-'+postId);

    var prevIds = [];
    var nextIds = [];
    var nextTitle = [];
    var prevTitle = [];
    var newTitle = [];

    var nextLi = _this.next();
    while (nextLi.length != 0 && nextLi.hasClass('visible')) {
        nextIds.push(nextLi.attr('id').split('photo-')[1]);
        nextLi = nextLi.next();
        if (nextIds.length >= 5) { break; }
    }

    var prevLi = _this.prev();
    while (prevLi.length != 0 && prevLi.hasClass('visible')) {
        prevIds.unshift(prevLi.attr('id').split('photo-')[1]);
        prevLi = prevLi.prev();
        if (prevIds.length >= 5) { break; }
    }

    jQuery('#loader').fadeIn(200, function() {

        jQuery.ajax({
            url: jQuery('#theme-url').attr('data-id'),
            data: {id : postId, next : next, prev : prev, prev_ids: prevIds, next_ids: nextIds, nextTitle: next_title, prevTitle:prev_title, title:newTitle },
            success: function(data) {

                var gallery = jQuery('#gallery-overlay');

                gallery.html(data);

                jQuery('#next-post').attr('data-id', next);

                jQuery('body').css({overflow: 'hidden'});

                jQuery('#gallery-overlay, #body-overlay').fadeIn(500, function () {
                    jQuery('#loader').fadeOut(500);
                });
                //re-initialize the gallery
                galleryInit();
                var url = postId;
                var sTit = title
                if(url) {// require a URL
                    window.location.hash = url + sTit;
                }
            }
        });

    });
}

这是html代码

<div id="gallery-wrap" class="clearfix">
  <ul class="large-grid">
    <li id="photo-417" class="term-9 visible">
      <a href="url" class="singleLightbox" data-title="data-title">
    <img" src="img" class="attachment-large-grid wp-post-image" alt="fotografia ślubna sesja nad polskim morzem" title="title" /></a></li>
    <li id="photo-333" class="term-4 visible">
       <a href="url" class="singleLightbox" data-title="data-title">
          <img src="img" class="attachment-large-grid wp-post-image" alt="sesja z trzymiesięcznym dzieckiem" title="title" /></a>
    </li>
  </ul>
 </div>
4

0 回答 0