Jquery Mobile:动态更改可折叠 div 标题的文本?

发布于 2024-12-11 10:51:02 字数 390 浏览 0 评论 0 原文

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

如何动态更改可折叠 div 中

标题('Place:')​​的文本?

我尝试过:

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

它改变了文本 - 但丢失了所有样式!

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

How can I dynamically change the text of the <h3> header ('Place:') in the collapsible div?

I tried:

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

And it changes the text - but loses all of the styling!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情话墙 2024-12-18 10:51:02

在 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");

快乐编码!

In 1.3.2, the following seem to work:

<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");

Happy Coding!

奢望 2024-12-18 10:51:02

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>

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

//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/

The actual HTML structure of a collapsible in jQuery Mobile 1.0RC2 is the following (after the framework has made its pass on the 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>

The nested <span> element within the <span class="ui-btn-text"> element makes this a little tricky. If you want to preserve the structure of the <span class="ui-btn-text"> element you will need to save the nested <span> element, overwrite it, and then replace it:

//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);
    });
});

Here is a jsfiddle of the above solution: http://jsfiddle.net/jasper/4DAfn/2/

来世叙缘 2024-12-18 10:51:02

一个简单的解决方案是

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

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

A simple solution would be

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

Check out the fiddle at http://jsfiddle.net/AQZQs/1/

梦境 2024-12-18 10:51:02

一个简单的选择是给 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');

An easy option is to give the h3 a custom id/class, for example:

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

That way you only will need to do:

$('#collapsePlace #h3Text').text('new text');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文