简体   繁体   English

AJAX正在子分区中加载

[英]AJAX Loading in Child Div

I'm having trouble explaining this but here it goes: 我在解释这个问题时遇到了麻烦,但是在这里:

I have a custom accordion script that's triggered when a user clicks on a link. 我有一个自定义手风琴脚本,当用户单击链接时会触发该脚本。 There are multiple accordions on each page, each with there their own link. 每页上都有多个手风琴,每个手风琴都有自己的链接。 This script works perfectly, each link only opening its correct accordion. 该脚本运行完美,每个链接仅打开其正确的手风琴。

Now, I need to modify this script to load content via AJAX in to a div within the accordion. 现在,我需要修改此脚本以通过AJAX将内容加载到手风琴中的div中。 I have the AJAX loading down, but instead of loading in to one DIV, it's loading in to every DIV with the same class. 我已经加载了AJAX,但是没有加载到一个DIV中,而是加载到了具有相同类的每个DIV中。 I understand why it's doing this, but I don't see how to tie it in with the existing script. 我知道为什么要这样做,但是我看不到如何将其与现有脚本结合在一起。 It uses the same button to load the content, so it should be fairly easy. 它使用相同的按钮加载内容,因此应该相当简单。

The accordion script uses the data-target attribute to specify which accordion to open, so that should be able to be tied in to the new script, but for the life of me I can't figure it out. 手风琴脚本使用data-target属性指定要打开的手风琴,因此应该能够绑定到新脚本,但是就我的生活而言,我无法弄清楚。

Here's what I have so far: 这是我到目前为止的内容:

HTML link: HTML链接:

<a class="button maximize accordionHeading" data-target="firstAccordion" href="/Load/?ID=1">More Info</a>

HTML div: HTML div:

<div class="accordionHidden firstAccordion">
    <div class="row" style="margin-top:10px;">
        <div class="six columns">
            <img class="load-image" src="images/dirt.jpg" />
        </div><!--/.six.columns-->
        <div class="six columns load-description">
            <!-- this is where the content should be loading -->
        </div><!--/.six.columns-->
    </div><!--/.row-->
</div><!--/.accordionHidden firstAccordion-->

Script: 脚本:

$("a.accordionHeading").bind("click", function(e){
    e.preventDefault();
    $("."+$(this).data("target")).slideToggle();
    $(this).addClass("minimize");
    if ($(this).text() == "More Info") {
       $(this).text("Less Info");
       $(this).removeClass("maximize").addClass("minimize");
    } else {
        $(this).addClass("maximize").removeClass("minimize").text("More Info");
    }

    // this is what I'm trying to add.
    var $loadID = $(this).attr("href");
    $(".load-description").load($loadID);

});

Based on the data-target of the anchors you can select the target divs by their class names, Try the following: 根据锚的data-target ,可以按其类名称选择目标div,请尝试以下操作:

var $loadID = $(this).attr("href");
var tgt = $(this).data('target')
$("." + tgt + " .load-description").load($loadID);

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

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