Jquery Slimbox 和 AJAX 图像交换问题

发布于 2024-11-08 23:22:15 字数 2046 浏览 0 评论 0原文

首先我要提到的是,我浏览了无数的问题和答案,其中许多在逻辑上似乎都是可行的。

我在 slimbox 和 AJAX 方面遇到问题。我正在执行简单的图像交换,当我这样做时,slimbox 将不适用于新添加的图像。

我尝试了很多方法,从调用 Live Query(jquery 插件)到简单地尝试重新绑定或再次调用 slimbox。

任何帮助或建议将不胜感激。也许将我的确切场景放入上下文中将有助于关联现有的解决方案之一来解决我的问题。到目前为止我还无法将它们合并起来。

第 1 步: 我的 php 代码生成了带有主图像的页面,对于 slimbox 效果很好:

<div id="productMainImage" class="centeredContent back">
    <a href="images/large/redwhiteandyou_LRG.jpg" rel="lightbox-g" title="Red White and You"><img src="images/medium/redwhiteandyou_MED.jpg" alt="Red White and You" title=" Red White and You " width="250" height="167"><br><span class="imgLink">larger image</span></a>
</div>

第 2 步: 我创建了一组属性图像,其中我我正在调用我的 ajax 代码来进行图像交换。这会进行一些处理,并基本上将#productMainImage的innerhtml设置如下:

<div id="productMainImage" class="centeredContent back">
    <a id="Yellow" href="images/large/attributes/redwhiteandyou_yellow_LRG.jpg" rel="lightbox-g" title="Yellow"><img src="images/medium/attributes/redwhiteandyou_yellow_MED.jpg" alt="Yellow" title=" Yellow " width="250" height="167"><br><span class="imgLink">larger image</span></a>
</div>

交换工作正常并且图像发生变化。最大的问题是我如何确保将该图像与 slimbox 链接起来。

我尝试过的几件事是(不限于!):

在ajax调用的代码中插入javascript以写出html:

$(document).ready(function() {
    $('#content').find("a[rel^='lightbox']").slimbox({}, null, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
});

或者

$("a[rel^='lightbox']").livequery(function(){
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
    return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    }), function() {
        //remove slimbox? this is called when elements no longer match
    }
});

我也尝试在我的页面中插入代码尝试类似的事情。

有人有什么建议吗?

First I'll mention that I have browsed through countless questions and answers, many of which would logically seem to work.

I am having an issue with slimbox and AJAX. I am performing a simple image swap and when I do so, slimbox will not work for the newly added image.

I tried many things from invoking Live Query (jquery addon) to simply trying to rebind or invoke slimbox again.

Any help or suggestions would be greatly appreciated. Maybe putting my exact scenario into context will help relate one of the solutions already out there to fix my problem. I've not been able to incorporate them thus far.

Step 1: I have my php code generating my page with a main image, for which slimbox works great:

<div id="productMainImage" class="centeredContent back">
    <a href="images/large/redwhiteandyou_LRG.jpg" rel="lightbox-g" title="Red White and You"><img src="images/medium/redwhiteandyou_MED.jpg" alt="Red White and You" title=" Red White and You " width="250" height="167"><br><span class="imgLink">larger image</span></a>
</div>

Step 2: I have a set of attribute images created, where I am invoking my ajax code to do the image swap. This does some processing and basically sets the innerhtml of #productMainImage as follows:

<div id="productMainImage" class="centeredContent back">
    <a id="Yellow" href="images/large/attributes/redwhiteandyou_yellow_LRG.jpg" rel="lightbox-g" title="Yellow"><img src="images/medium/attributes/redwhiteandyou_yellow_MED.jpg" alt="Yellow" title=" Yellow " width="250" height="167"><br><span class="imgLink">larger image</span></a>
</div>

The swap works fine and the image changes. The big question is how I can make sure to link that image up with slimbox.

A couple of thing things I've tried are (not limited to!):

Inserting javascript in the code called by ajax to write out the html:

$(document).ready(function() {
    $('#content').find("a[rel^='lightbox']").slimbox({}, null, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
});

OR

$("a[rel^='lightbox']").livequery(function(){
    $("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
    return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    }), function() {
        //remove slimbox? this is called when elements no longer match
    }
});

I have also tried inserting code in my page attempting similar things.

Anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-11-15 23:22:15

好吧,我继续尝试解决这个问题,发现我做错了什么。 AJAX 如何更新页面的流程中存在一个核心误解。我将 slimbox 调用插入到 AJAX 代码中的以下函数中,现在它可以工作了!

function stateChanged() { 
  if (xmlHttp.readyState==4){    
    var product_color_image=xmlHttp.responseText;
    if(product_color_image!=''){
    document.getElementById('productMainImage').innerHTML = product_color_image;
    }
    $("a[rel^='lightbox']").slimbox({
      <?php require_once(DIR_FS_CATALOG . DIR_WS_CLASSES . 'zen_lightbox/options.php'); ?>
    }, function(el){
      return [el.href, el.title /* + '<br /><a href="' + el.href + '">Download this image</a>'*/];
       }, function(el) {
            return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
          }), function() {
                //remove slimbox? this is called when elements no longer match
              }
    }
}

我需要处理一些更精细的细节,但至少现在在我对主产品图像执行 AJAX 交换后,我的图像可以用灯箱显示!

Well, I continued to try to solve this and found what I was doing wrong. A core misunderstanding in the flow of how AJAX was updating the page. I plugged the slimbox call into the following function in the AJAX code and it works now!

function stateChanged() { 
  if (xmlHttp.readyState==4){    
    var product_color_image=xmlHttp.responseText;
    if(product_color_image!=''){
    document.getElementById('productMainImage').innerHTML = product_color_image;
    }
    $("a[rel^='lightbox']").slimbox({
      <?php require_once(DIR_FS_CATALOG . DIR_WS_CLASSES . 'zen_lightbox/options.php'); ?>
    }, function(el){
      return [el.href, el.title /* + '<br /><a href="' + el.href + '">Download this image</a>'*/];
       }, function(el) {
            return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
          }), function() {
                //remove slimbox? this is called when elements no longer match
              }
    }
}

I need to work through a few of the finer details, but at least now my images are displaying with a lightbox after I've performed an AJAX swap on the main product image!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文