Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用bbq和jquery在url中显示下一个和上一个标题_Jquery_Url_Url Rewriting_Hashchange_Jquery Bbq - Fatal编程技术网

如何使用bbq和jquery在url中显示下一个和上一个标题

如何使用bbq和jquery在url中显示下一个和上一个标题,jquery,url,url-rewriting,hashchange,jquery-bbq,Jquery,Url,Url Rewriting,Hashchange,Jquery Bbq,任何人都可以帮我,我有图库,每个li都有数据标题,我想把那个值添加到url,我尝试着不走运,它总是说未定义,但我有帖子id,我很容易就把它添加到了url。任何人都知道如何将数据标题属性传递给url function getPhoto(postId, hasSelector) { //first visible value var title = jQuery(this).children("li").attr("data-title"); var next =

任何人都可以帮我,我有图库,每个li都有数据标题,我想把那个值添加到url,我尝试着不走运,它总是说未定义,但我有帖子id,我很容易就把它添加到了url。任何人都知道如何将数据标题属性传递给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>


能否显示
HTML
.children()
为您提供选择器的直接子级。您正在尝试获取它的属性。。使用
$。而不是每个
,并获取每个li的属性。。您还可以使用此语法访问数据属性
.data('title')
我已经试过了,但它仍然没有定义。你可以发布一个小的工作示例,说明哪些东西不起作用,也许是用小提琴