<div data-role="collapsible" data-content-theme="e" id="collapsePlace">
<h3>Place:</h3>
<!--things...-->
</div>
如何动态更改<h3>
可折叠 div 中标题的文本('Place:')?
我试过:
$('#collapsePlace').children('h3').text('new text');
它会更改文本 - 但会丢失所有样式!
<div data-role="collapsible" data-content-theme="e" id="collapsePlace">
<h3>Place:</h3>
<!--things...-->
</div>
如何动态更改<h3>
可折叠 div 中标题的文本('Place:')?
我试过:
$('#collapsePlace').children('h3').text('new text');
它会更改文本 - 但会丢失所有样式!
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>
<span>
元素内的嵌套元素<span class="ui-btn-text">
使这有点棘手。如果要保留<span class="ui-btn-text">
元素的结构,则需要保存嵌套<span>
元素,覆盖它,然后替换它:
//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/
在 1.3.2 中,以下似乎有效:
<div id="MyID" data-role="collapsible" >
<h3><span id="MyHeaderID">My Header</span></h3>
<!-- My Content -->
</div>
查询:
$("#MyID h3 #MyHeaderID").text("Your New Header");
快乐编码!
一个简单的选择是给 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');