JavaScript 中的 Rel 属性

发布于 2024-11-23 15:20:53 字数 895 浏览 0 评论 0原文

我有灯箱画廊视图脚本并且工作正常。

在我的网站中,我使用 ajax 脚本调用另一个页面并将其加载到 DIV 中,

例如:

ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4){
    var ajaxDisplay = document.getElementById('loadmailfunctions');
    ajaxDisplay.innerHTML = ajaxRequest.responseText; sizeit();
    setVisibility('uploads', 'block'); settotime();
  }
}
ajaxRequest.open("GET", "profile/photos.inc.php", true);
ajaxRequest.send(null); 

然后加载我需要的所有照片,但是当我单击照片并使用 rel="lightbox" 时,它不起作用。

这是否与父 DOM/窗口有关?抱歉,我是个菜鸟,但我希望有人能指出我正确的方向?

编辑:

在加载的页面 profile/photos.inc.php 中,我有以下代码:

echo '<li><a href="',$row['localdirectory'],$row['photo'],'" rel="lightbox" ><img src="',$row['localdirectory'],$row['photo'],'" style="width:110px; height:85px;" alt="" /></a></li>'

但不执行 Lightbox 查看器脚本。

I have the lightbox gallery view script and is working fine.

In my site I use an ajax script to call another page and load it into a DIV

For Example:

ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4){
    var ajaxDisplay = document.getElementById('loadmailfunctions');
    ajaxDisplay.innerHTML = ajaxRequest.responseText; sizeit();
    setVisibility('uploads', 'block'); settotime();
  }
}
ajaxRequest.open("GET", "profile/photos.inc.php", true);
ajaxRequest.send(null); 

This then loads all the photos I need but when I click on a photo and use the rel="lightbox" it does not work.

Is this something to do with the parent DOM / window or something? Sorry bit of a noob, but I was hoping someone could point me in the right direction?

EDIT:

In the loaded page profile/photos.inc.php, I have this code:

echo '<li><a href="',$row['localdirectory'],$row['photo'],'" rel="lightbox" ><img src="',$row['localdirectory'],$row['photo'],'" style="width:110px; height:85px;" alt="" /></a></li>'

But does not execute the Lighbox viewer script.

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

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

发布评论

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

评论(2

早乙女 2024-11-30 15:20:53

Lightbox 在页面加载后激活,它扫描 DOM 树,搜索具有 rel="lightbox" 属性的锚点,并将 onclick 事件绑定到它们。当您执行 AJAX 调用时,新元素会添加到 DOM 树中,但您必须手动初始化扫描新锚点。

Lightbox activates after page load, it scans DOM tree searching for anchors with rel="lightbox" attribute and binds onclick event to them. When you perform AJAX call, new elements are added to DOM tree, but you have to manually initialize scanning for new anchors.

扶醉桌前 2024-11-30 15:20:53

正如 dev-null-dweller 所暗示的,Lightbox 使用示例仅在页面加载时激活 rel="lightbox" 链接:

<script type="text/javascript">
$(function() {
   $('a[@rel*=lightbox]').lightBox({
    overlayBgColor: '#FFF',
    overlayOpacity: 0.6,
    imageLoading: 'http://example.com/images/loading.gif',
    imageBtnClose: 'http://example.com/images/close.gif',
    imageBtnPrev: 'http://example.com/images/prev.gif',
    imageBtnNext: 'http://example.com/images/next.gif',
    containerResizeSpeed: 350,
    txtImage: 'Imagem',
    txtOf: 'de'
   });
});
</script>

您应该为神奇地出现在 div 中的新元素重新运行此示例 after Ajaxifying:

<script type="text/javascript">
var lightBoxOptions = {
    overlayBgColor: '#FFF',
    overlayOpacity: 0.6,
    imageLoading: 'http://example.com/images/loading.gif',
    imageBtnClose: 'http://example.com/images/close.gif',
    imageBtnPrev: 'http://example.com/images/prev.gif',
    imageBtnNext: 'http://example.com/images/next.gif',
    containerResizeSpeed: 350,
    txtImage: 'Imagem',
    txtOf: 'de'
};

$(function() {
   // Set Lightbox handlers on "static" elements
   $('a[@rel*=lightbox]').lightBox(lightBoxOptions);
});

// then, whereever your AJAX code is:
ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4){
    var ajaxDisplay = document.getElementById('loadmailfunctions');
    ajaxDisplay.innerHTML = ajaxRequest.responseText; sizeit();
    setVisibility('uploads', 'block'); settotime();

    // Set Lightbox handlers on new elements
    $('#loadmailfunctions a[@rel*=lightbox]').lightBox(lightBoxOptions);
  }
}
</script>

顺便说一句,如果您正在使用 lightbox jQuery 插件(我必须假设您是这样,因为您没有另外指定),为什么您不整体使用 jQuery?

如果您使用 jQuery 插件,则以类似的方式重复您的事件处理程序设置。

As dev-null-dweller implied, the Lightbox usage example only activates rel="lightbox" links on page load:

<script type="text/javascript">
$(function() {
   $('a[@rel*=lightbox]').lightBox({
    overlayBgColor: '#FFF',
    overlayOpacity: 0.6,
    imageLoading: 'http://example.com/images/loading.gif',
    imageBtnClose: 'http://example.com/images/close.gif',
    imageBtnPrev: 'http://example.com/images/prev.gif',
    imageBtnNext: 'http://example.com/images/next.gif',
    containerResizeSpeed: 350,
    txtImage: 'Imagem',
    txtOf: 'de'
   });
});
</script>

You should re-run this for new elements that have magically appeared in your div after Ajaxifying:

<script type="text/javascript">
var lightBoxOptions = {
    overlayBgColor: '#FFF',
    overlayOpacity: 0.6,
    imageLoading: 'http://example.com/images/loading.gif',
    imageBtnClose: 'http://example.com/images/close.gif',
    imageBtnPrev: 'http://example.com/images/prev.gif',
    imageBtnNext: 'http://example.com/images/next.gif',
    containerResizeSpeed: 350,
    txtImage: 'Imagem',
    txtOf: 'de'
};

$(function() {
   // Set Lightbox handlers on "static" elements
   $('a[@rel*=lightbox]').lightBox(lightBoxOptions);
});

// then, whereever your AJAX code is:
ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4){
    var ajaxDisplay = document.getElementById('loadmailfunctions');
    ajaxDisplay.innerHTML = ajaxRequest.responseText; sizeit();
    setVisibility('uploads', 'block'); settotime();

    // Set Lightbox handlers on new elements
    $('#loadmailfunctions a[@rel*=lightbox]').lightBox(lightBoxOptions);
  }
}
</script>

Incidentally, if you're using the lightbox jQuery plugin (and I have to assume that you are, since you haven't specified otherwise), why aren't you using jQuery as a whole?

If you're not using the jQuery plugin, then repeat your event handler setup in a similar way.

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