简体   繁体   English

手风琴+附加内容Jquery

[英]Accordion + Appended Content Jquery

So I have an accordion menu that I created with Jquery: 所以我有一个我用Jquery创建的手风琴菜单:

    <script>
$(document).ready(function() {
/*Accordian Script for the Request New Appraisal Panel*/
$('.accordian_item').hide();
$('.accordian_item').first().slideDown();

$('.accordian_trigger').click(function(event) {
    event.preventDefault();
    $(this).parent().find('.accordian_item').slideToggle();
});
});
</script>

Now, I want to be able to dynamically append extra accordion items to the accordion box, which I have done like this: 现在,我希望能够动态地将额外的手风琴项添加到手风琴盒中,我这样做了:

<script>

$('#add_contact_btn').click(function(event) {
    event.preventDefault();

    var large = '<div class="accordian_container"><a href="#" class="accordian_trigger"><h4>Co-Borrower Information</h4></a><hr/><div class="accordian_item"><label> First Name</label><br/><input type="text"/><br/><label>Middle Name</label><br/><input type="text"/><br/><label>Last Name</label><br/><input type="text" /><br/><label>Home Number</label><br/><input type="text"/><br><label>Work Number</label><br/><input type="text"/><br><label>Cell Number</label><br/><input type="text"/><br></div></div>';

    $('#accordion_container_box').append(large);


});
</script>

This works perfect, except the dynamically generated items don't collaps when you click on the collapse button. 这是完美的,除了单击折叠按钮时动态生成的项目不会折叠。 The existing accordion items still work. 现有的手风琴项目仍然有效。 For some reason it seems like Jquery wont trigger for dynamically created links. 出于某种原因,似乎Jquery不会触发动态创建的链接。 Any ideas how I can correct this? 任何想法我怎么能纠正这个?

BTW, here is the basic HTML structure: 顺便说一句,这是基本的HTML结构:

<div id="accordion_container_box">

            <div class="accordian_container">
                <a href="#" class="accordian_trigger"><h4>Borrower's Information</h4></a>
                <hr/>
                <div class="accordian_item">
                <label> First Name</label><br/>
                <input type="text"/><br/>
                <label>Middle Name</label><br/>
                <input type="text"/><br/>
                <label>Last Name</label><br/>
                <input type="text" /><br/>
                <label>Home Number</label><br/>
                <input type="text"/><br>
                <label>Work Number</label><br/>
                <input type="text"/><br>
                <label>Cell Number</label><br/>
                <input type="text"/><br>
                </div>
            </div>

            <div class="accordian_container">       
                <a href="#" class="accordian_trigger"><h4>Co-Borrower's Information</h4></a>
                <hr/>
                <div class="accordian_item">
                <label> First Name</label><br/>
                <input type="text"/><br/>
                <label>Middle Name</label><br/>
                <input type="text"/><br/>
                <label>Last Name</label><br/>
                <input type="text" /><br/>
                <label>Home Number</label><br/>
                <input type="text"/><br>
                <label>Work Number</label><br/>
                <input type="text"/><br>
                <label>Cell Number</label><br/>
                <input type="text"/><br>
                </div>
            </div>

        </div>

            <a href="#" id="add_contact_btn">+ Additional Contact</a>

你必须在动态创建的内容上使用.live('click'...)。

By default binding events to elements will only affect nodes that exist at the time that the bind runs. 默认情况下,将事件绑定到元素只会影响绑定运行时存在的节点。 By using event delegation you can make use of the built in way that events bubble. 通过使用事件委派,您可以利用事件冒泡的内置方式。 jQuery < 1.7 did this with the delegate and live methods, jQuery 1.7 added the on method to consolidate all the event handlers in a single API. jQuery <1.7使用delegatelive方法做到了这一点,jQuery 1.7添加了on方法来整合单个API中的所有事件处理程序。

For example you could use the following to handle all clicks on nodes with a class of accordian_trigger regardless of when they were created. 例如,您可以使用以下方法处理具有类accordian_trigger节点上的所有单击,无论它们何时创建。

​$(document).on('click', '.accordian_trigger', function() {
    //whatever you need to do
});​​​​​​​​​​​

What this will do is attach an onclick event handler to the document itself. 这将做的是将onclick事件处理程序附加到document本身。 When any click occurs in the DOM it will bubble up from the node that the event occurred on to its parent and its parent until it reaches the document . 当DOM中发生任何单击时,它将从事件发生的节点冒泡到其父节点及其父节点,直到它到达document jQuery will then check whether the event occurred on a node that matches the selector passed in as the second parameter of on , in this case it will check whether the node has a class of accordian_trigger . 然后,jQuery将检查事件是否发生在与传入的选择器匹配的节点上作为on的第二个参数,在这种情况下,它将检查该节点是否具有accordian_trigger类。 If it does it will run the function passed in as the third parameter. 如果是,它将运行作为第三个参数传入的function

For efficiency's sake you'll likely want to replace document with a parent node that you know will contain all accordian_trigger nodes. 为了提高效率,您可能希望将document替换为您知道将包含所有accordian_trigger节点的父节点。 Otherwise all clicks bubble all the way up to the document and check whether the node that was clicked on has the accordian_trigger class, which is potentially expensive, especially if you have a large DOM. 否则, 所有点击都会一直冒泡到document并检查被点击的节点是否具有accordian_trigger类,这可能很昂贵,特别是如果你有一个大的DOM。

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

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