0

我正在使用 jQuery 从 XML 文件中获取结果并在 div 中显示每个条目。这是可行的,但现在我想这样做,以便如果我单击 div,它会在灯箱中显示来自 xml 的更多数据。

我在这里创建了一个 jsfiddle ,但它不起作用:

这是我的html:

<section id="page3">
    <section class="portfolio row">
        <! Hier komen cases uit xml !>
                <div id="clearfix"></div>
            </section>
            <section class="portfolio_detail">
               <span class="prev">&lt;</span>
               <section class="ajax_image"></section>
               <span class="next">&gt;</span>
               <span class="close">X</span>
               <section class="watermark">
               <p>Thomas More &copy; Insilencio<br>
               Corporate identity maximum two lines</p>
               </section>
            </section>
        </section>

这是我的 jQuery:

    $.ajax({
    type: 'GET',
    url: "hellojames.commefort.com/onepager/data.xml",
    dataType: "xml",
    success: function (data) {
        //console.log(data);
        $(data).find("case").each(function () {
            var dezeCase = $(this);
            var id = dezeCase.find("id").text();
            var titel = dezeCase.find("titel").text();
            var thumb = dezeCase.find("thumb").text();
            var full = dezeCase.find("full");
            var images = full.find('img');
            var imagesArray = images;
            var html = '<div class="portfolio_item col" id="' + titel + '"><img src="' + thumb + '" alt="' + titel + '"><p class="caption">' + titel + '</p></div>';

            $('.portfolio').prepend(html);
            mouseHover();
            caseClick(imagesArray);
        });
    }
});

function mouseHover() {
    $('.portfolio_item').mouseover(function () {
        $(this).children('img').addClass("hover");
        $(this).children('.caption').show();
        //console.log('hover');
    }).mouseout(function () {
        $(this).children('img').removeClass("hover");
        //console.log('stophover');
        $('.caption').hide();
    });
};

function caseClick(afbeeldingen) {
    /* Detailpagina */
    $('.portfolio_item').on('click', function () {
        var lightbox = $('.portfolio_detail');
        $(".ajax_image").empty();
        lightbox.show().children().hide();
        lightbox.animate({
            width: '100%',
            height: "100%"
        }, 'fast');
        lightbox.children().fadeIn('slow');
        var inhoud = "<img src='" + afbeeldingen + "'>";
        $(".ajax_image").append(inhoud);
    });

};
4

2 回答 2

0

不确定,但这可能会有所帮助:

由于您的 div.portfolio_item是动态生成的,因此您必须delegate the event to the existing closest parent elem或者simply delegate to the document itself哪个是页面上所有元素的父级。

function caseClick(afbeeldingen) {
  $(document).on('click', '.portfolio_item', function () {
      var lightbox = $('.portfolio_detail');
      $(".ajax_image").empty();
      lightbox.show().children().hide();
      lightbox.animate({
        width: '100%',
        height: "100%"
      }, 'fast');
      lightbox.children().fadeIn('slow');
      var inhoud = "<img src='" + afbeeldingen + "'>";
      $(".ajax_image").append(inhoud);
  });
} //<-----------';' not required same applies to function mouseover(){}

你应该;在什么时候使用

var caseClick = function(afbeeldingen){
    // all your code stuff
}; // <----------------------here `;` needed

制作这样的功能。

于 2013-02-07T08:43:26.817 回答
0
var $body=$(document.body); 
$body.on({

  'mouseover':function(e) {
    //..
      }
  ,'mouseout':function(e) {//...
   }

  ,'click', function () {
    var lightbox = $('.portfolio_detail');
    $(".ajax_image").empty();
    lightbox.show().children().hide();
    lightbox.animate({
        width: '100%',
        height: "100%"
    }, 'fast');
    lightbox.children().fadeIn('slow');
    var inhoud = "<img src='" + $(this).data("imgsrc") + "'>";
    $(".ajax_image").append(inhoud);
}
, '.portfolio_item');


 $.ajax({
type: 'GET',
url: "hellojames.commefort.com/onepager/data.xml",
dataType: "xml",
success: function (data) {
    var html='';
     //console.log(data);
    $(data).find("case").each(function () {
        var dezeCase = $(this);
        var id = dezeCase.find("id").text();
        var titel = dezeCase.find("titel").text();
        var thumb = dezeCase.find("thumb").text();
        var full = dezeCase.find("full");


        html += '<div class="portfolio_item col" data-imgsrc="'+full.find('img')+'" id="' + titel + '"><img src="' + thumb + '" alt="' + titel + '"><p class="caption">' + titel + '</p></div>';



    });
    $(html).prependTo($('.portfolio'));
}
});

为了优化它,在添加/添加之前将您的项目包装在一个容器中

于 2013-02-07T08:48:56.200 回答