【问题标题】:Jquery Mobile: Dynamically change the text of the header of a collapsible div?Jquery Mobile:动态更改可折叠 div 标题的文本?
【发布时间】:2011-10-22 03:33:34
【问题描述】:
<div data-role="collapsible" data-content-theme="e" id="collapsePlace">
    <h3>Place:</h3>
    <!--things...-->
</div>

如何动态更改可折叠 div 中 &lt;h3&gt; 标头 ('Place:') 的文本?

我试过了:

$('#collapsePlace').children('h3').text('new text');

它会更改文本 - 但会丢失所有样式!

【问题讨论】:

标签: jquery jquery-mobile collapsable


【解决方案1】:

jQuery Mobile 1.0RC2 中可折叠的实际 HTML 结构如下(在框架通过 HTML 之后):

<div id="collapsePlace" data-content-theme="e" data-role="collapsible" class="ui-collapsible ui-collapsible-collapsed">
    <h3 class="ui-collapsible-heading ui-collapsible-heading-collapsed">
        <a class="ui-collapsible-heading-toggle ui-btn ui-btn-icon-left ui-corner-top ui-corner-bottom ui-btn-up-c" href="#" data-theme="c">
            <span aria-hidden="true" class="ui-btn-inner ui-corner-top ui-corner-bottom">
                <span class="ui-btn-text">Place:
                    <span class="ui-collapsible-heading-status"> click to expand contents</span>
                </span>
                <span class="ui-icon ui-icon-plus ui-icon-shadow"></span>
            </span>
        </a>
    </h3>
    <div class="ui-collapsible-content ui-body-e ui-collapsible-content-collapsed" aria-hidden="true">
        <!--things...-->
    </div>
</div>

&lt;span class="ui-btn-text"&gt; 元素中嵌套的&lt;span&gt; 元素使这有点棘手。如果要保留 &lt;span class="ui-btn-text"&gt; 元素的结构,则需要保存嵌套的 &lt;span&gt; 元素,覆盖它,然后替换它:

//run code on document.ready
$(function () {
    var num = 1;
    //add click handler to link to increment the collapsible's header each click
    $('a').bind('click', function () {
        //cache the `<span class="ui-btn-text">` element and its child
        var $btn_text  = $('#collapsePlace').find('.ui-btn-text'),
            $btn_child = $btn_text.find('.ui-collapsible-heading-status');
        //overwrite the header text, then append its child to restore the previous structure
        $btn_text.text('New String (' + num++ + ')').append($btn_child);
    });
});

这是上述解决方案的一个 jsfiddle:http://jsfiddle.net/jasper/4DAfn/2/

【讨论】:

    【解决方案2】:

    在 1.3.2 中,以下似乎有效:

    <div id="MyID" data-role="collapsible" >    
        <h3><span id="MyHeaderID">My Header</span></h3>    
        <!-- My Content --> 
    </div>
    

    jQuery:

    $("#MyID h3 #MyHeaderID").text("Your New Header");
    

    编码愉快!

    【讨论】:

    • 正在搜索这个。太好了!
    【解决方案3】:

    一个简单的解决方案是

    $('#collapsePlace .ui-btn-text').text("hello ");
    

    http://jsfiddle.net/AQZQs/1/查看小提琴

    【讨论】:

    • 这将从问题 HTML 中显示的.ui-btn-text 元素中删除 HTML。
    • 对于更高版本的 jQuery 移动版本(使用 v 1.4.3 测试),将“.ui-btn-text”替换为“.ui-collapsible-heading-toggle”
    【解决方案4】:

    一个简单的选择是给 h3 一个自定义 id/class,例如:

    <div data-role="collapsible" data-content-theme="e" id="collapsePlace">
        <h3 id='h3Text'>Place:</h3>
        <!--things...-->
    </div>
    

    这样你只需要这样做:

    $('#collapsePlace #h3Text').text('new text');
    

    【讨论】:

    • 我不知道它是否适用于以前版本的 JQM 但它不适用于 1.2.0
    • 正如您在这个小提琴中看到的那样,尽管文本发生了变化,但样式正在被删除。 jsfiddle.net/AQZQs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多