jQueryMobile:如何动态地向Collapsible Div添加内容

时间:2013-03-05 06:55:50

标签: jquery-mobile

我有一个可折叠的div,并且在那2个可折叠的div中。

当我点击加号按钮时,我必须动态地向这些可折叠div添加元素。我尝试了很多代码,但没有用。你能告诉我怎么做吗?

HTML代码

<div data-role="content">
 <div data-role="collapsible-set" id="setAllCategory">
     <div data-role="collapsible" data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u" id="setCategory1">
        <h1 ><label>Food</label>
        <a data-role="button" id="btnAddSubCat1" name="btnAddSubCat1"  data-rel="popup"  href="#" data-icon="plus" data-iconpos="notext" style="float:right;" ><a/></h1>
     </div>

     <div data-role="collapsible" data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u" id="setCategory2">
        <h1 ><label>Dress</label>
        <a data-role="button" id="btnAddSubCat2" name="btnAddSubCat2"  data-rel="popup"  href="#" data-icon="plus" data-iconpos="notext" style="float:right;" ><a/></h1>
     </div>
 </div>
</div>

的JavaScript


var subCatObject = "<div class='ui-grid-b'>"
subCatObject        +="<div class='ui-block-a'><label>Hi</label></div>"
subCatObject        +="<div class='ui-block-b'><a href='#' data-role='button' data-iconpos='notext' data-icon='edit'></a></div>"
subCatObject        +="<div class='ui-block-c'><a href='#' data-role='button' data-iconpos='notext' data-icon='delete'></a></div>"
subCatObject        +="</div>";


    $('#btnAddSubCat1').click(function (){
           var temp = $(subCatObject);
           $(temp).appendTo('#setCategory1').page();
           $("#setCategory1").collapsibleset('refresh');
    });

1 个答案:

答案 0 :(得分:1)

我更改了您的HTML

<div data-role="page" id="home">
    <div data-role="content">
        <div data-role="collapsible-set" id="setAllCategory" data-content-theme="d">
            <div data-role="collapsible" data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u"
            id="setCategory1">
                 <h1><label>Food</label>
        <a data-role="button" id="btnAddSubCat1" name="btnAddSubCat1"  data-rel="popup"  href="#" data-icon="plus" data-iconpos="notext" style="float:right;" ><a/></h1>

                <div id="setCategoryContent1"></div>
            </div>
            <div data-role="collapsible" data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u"
            id="setCategory2">
                 <h1><label>Dress</label>
        <a data-role="button" id="btnAddSubCat2" name="btnAddSubCat2"  data-rel="popup"  href="#" data-icon="plus" data-iconpos="notext" style="float:right;" ><a/></h1>

            </div>
        </div>
    </div>
</div>

和你的javascript如下

var subCatObject = "<div class='ui-grid-b'>"
subCatObject += "<div class='ui-block-a'><label>Hi</label></div>"
subCatObject += "<div class='ui-block-b'><a href='#' data-role='button' data-iconpos='notext' data-icon='edit'></a></div>"
subCatObject += "<div class='ui-block-c'><a href='#' data-role='button' data-iconpos='notext' data-icon='delete'></a></div>"
subCatObject += "</div>";

$('#btnAddSubCat1').bind('click', function () {
 $("#setCategoryContent1").html(subCatObject);
 $('#home').trigger('create');
});

我希望你能从这里接受。

在这里查看工作小提琴http://jsfiddle.net/PKeSB/2/