简体   繁体   English

通过Ajax加载HTML / LINK时,jQueryTOOLS重叠式灯箱不起作用

[英]jQueryTOOLS Overlay lightbox not functioning when HTML/LINK is loaded via ajax

Before starting, I have researched and found this similar, previous question: Reinitialize jQuerytools Overlay on ajax loaded element 在开始之前,我已经研究并找到了类似的先前问题: 重新初始化jQuerytools在ajax加载的元素上覆盖

However, the "answer" to that question suggests using .live(). 但是,对该问题的“答案”建议使用.live()。 I attempted to us jQuery 1.9 .live() is not a function to update using .on(). 我尝试向我们提供jQuery 1.9 .live()不是使用.on()更新的功能

$(".overlay_load[rel]").on('click', 'a', function () {
    $(this).overlay().load();
    $(this).overlay().load();
    return false;
});

I will admit I don't 100% understand exactly what that is doing other than perhaps re-initializing the newly loaded HTML so that jQueryTOOLS Overlay has 'access' to the newly loaded HTML. 我会承认,除了重新初始化新加载的HTML以便jQueryTOOLS Overlay可以“访问”新加载的HTML之外,我不是100%确切地了解正在做什么。

I have a page loading sections of HTML content via AJAX. 我有一个页面通过AJAX加载HTML内容部分。 Here is an example of one DIV that is loaded. 这是一个加载的DIV的示例。

<div class="ajax-returned mytop">
    <span class="username"> drkwong </span> <br> 
    <span class="full-name">Andrew Kwong</span><br> 
    <span class="action-status"> 
        <span class="mt-icon ut3"></span> 
        <span class="autoship active">A</span>
        <span class="view_profile">
            <a href="/member/downline_tree_view/?view=tree_profile&amp;program=1&amp;view_userid=13" rel="#overlay">
                <img src="/inc/downline_tree/images/more_info.png" rel="#13">
            </a>
        </span> 
    </span>
</div>

<!-- overlayed element -->
<div class="apple_overlay" id="overlay">
    <!-- the external content is loaded inside this tag -->
    <div class="contentWrap"></div>
</div>

This has a link that should engage a jQuery and then load a jQueryTOOLS Overlay lightbox: 这有一个链接,该链接应使用jQuery,然后加载jQueryTOOLS叠加灯箱:

    <script>
            $(function() {
                // if the function argument is given to overlay,
                // it is assumed to be the onBeforeLoad event listener
                $("a[rel]").overlay({

                    mask: 'grey',
                    effect: 'apple',

                    onBeforeLoad: function() {

                        // grab wrapper element inside content
                        var wrap = this.getOverlay().find(".contentWrap");

                        // load the page specified in the trigger
                        wrap.load(this.getTrigger().attr("href"));
                    }
                });
            });
    </script>

This is the jQuery I am using to load an external page into a Light Box: http://jquerytools.github.io/demos/overlay/external.html 这是我用来将外部页面加载到灯箱中的jQuery: http : //jquerytools.github.io/demos/overlay/external.html

It simply feels like it's not triggering the jQuery. 感觉就像没有触发jQuery。

Here's what's in the header: 这是标题中的内容:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="/templates/public/2/downline/js/jquery.browser.min.js"></script>
<script type="text/javascript" src="/templates/public/2/downline/js/jquery.tools.min.js"></script>

The jquery.browser.min.js is required by jQueryTOOLS Overlay to access browsers. jQueryTOOLS Overlay需要使用jquery.browser.min.js来访问浏览器。 https://github.com/gabceb/jquery-browser-plugin https://github.com/gabceb/jquery-browser-plugin

EDIT: 编辑:

AJAX AJAX

var ids=new Array()
var node
var param={"action":"NodeDetailAll","admin":"1"}
var users=new Array()



function get_ids(){
    for(var i=0;i<nodes.length;i++){
        users.push("child_"+$(nodes[i]).attr("class"))
        ids.push($(nodes[i]).attr("class"))
    }
}

function get_nodes(){
    nodes = $("#chart [myattr*='descendent']")
    nodes= jQuery.makeArray(nodes);

    $.when(get_ids()).then(function(){
        $.each(ids,function(key,value){
            param[users[key]]=value
        })
    })        
}

function write_html(response){
    $.each(response,function(key,value){
      $("."+key).html(value)
    })
}

jQuery(document).ready(function() {
    $(".spinner-loader").fadeIn(200)

    $.when(get_nodes()).then(function(){

        $.post("~ajax_url~",param,function(response) { 

            response=jQuery.parseJSON(response)
            $.when(write_html(response)).done(function() {
                $(".spinner-loader").fadeOut(600)
                $("#chart").css("opacity","1")

                // is this where I would add the on()? //
                // I tried that, without success.  //
                // perhaps I need to modify the on() function? //

            })
        })
    })
})

You could call the overlay binding on the callback of your ajax function. 您可以在ajax函数的回调中调用覆盖绑定。 The other thing would be is to check your jQuery selectors to make sure you have them configured appropriately and that they are selecting the appropriate element to bind your event to. 另一件事是检查您的jQuery选择器,以确保已正确配置它们,并选择了将事件绑定到的适当元素。

As per the jQuery documentation: "Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on()." 按照jQuery文档的规定:“事件处理程序仅绑定到当前选定的元素;在代码调用.on()时,它们必须存在于页面上。”

so it does seem to me that calling the binding functionality in the callback of your AJAX request would be the way to go! 因此在我看来,在您的AJAX请求的回调中调用绑定功能将是可行的方法! Perhaps if you could provide a simplified version of your code, ajax request included then I could try and take another look :) 也许如果您可以提供代码的简化版本,包括ajax请求,那么我可以尝试再看看:)

Use a different solution. 使用其他解决方案。 jQueryTOOLS Overlay is no longer active and uses deprecated and even eliminated jQuery functions. jQueryTOOLS Overlay不再处于活动状态,并使用已弃用甚至取消的jQuery函数。 Trying to solve all those issues has proven both inefficient and so far as AJAX is related, impossible with the latest versions of jQuery. 就所有这些问题而言,试图解决所有这些问题都被证明效率低下,而且就AJAX而言,这是最新版本的jQuery无法实现的。

As suggested by @sparky FancyBox 2 provided the needed solution. 如@sparky所建议,FancyBox 2提供了所需的解决方案。 It was simple and intuitive to implement. 实施起来既简单又直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM