简体   繁体   English

jQuery Mobile创建错误的HTML代码

[英]JQuery Mobile creates wrong HTML code

I'm currently developing a PhoneGap Application for Android using JQuery Mobile. 我目前正在使用JQuery Mobile开发适用于Android的PhoneGap应用程序。 All i want, is to dynamically change the header text of a collapsible: 我想要的是动态更改可折叠的标题文本:

<div data-role="collapsible" id="collapse">
   <h3>I'm a header</h3>
   <p>I'm the collapsible content</p>
</div>

That code is from demos.jquerymobile.com. 该代码来自demos.jquerymobile.com。 Chrome DevTools gives me the following html for this example: http://de.tinypic.com/r/2uf982p/8 Chrome DevTools为该示例提供了以下html: http ://de.tinypic.com/r/2uf982p/8

For any reason, if i copy exactly the same code to my index.html and run it, chrome DevTools gives me the following html: http://de.tinypic.com/r/2wohcf7/8 出于任何原因,如果我将完全相同的代码复制到我的index.html并运行它,chrome DevTools会为我提供以下html: http ://de.tinypic.com/r/2wohcf7/8

Why are there different html codes? 为什么会有不同的html代码?

I actually can change the header's text by 我实际上可以通过更改标题的文本

$("#collapse").text("new text");

but then it looses all the styling and also 但随后它失去了所有样式,并且

$("#collapse").trigger('create').

doesn't do anything. 什么也没做 What am I doing wrong? 我究竟做错了什么?

You need to target the <h3> tag of the div itself. 您需要定位div本身的<h3>标签。 Consider what you've changed: 考虑一下您所做的更改:

$("#collapse").text("new text");

This replaces all of the text within the collapse id. 这将替换折叠ID中的所有文本。 What you need to do instead is grab the value of the header itself, which can be done as follows: 相反,您需要获取标头本身的值,可以按照以下步骤进行操作:

<div data-role="collapsible" id="collapse">
   <h3 id="collapse-header">I'm a header</h3>
   <p>I'm the collapsible content</p>
</div>
<script>
    $('#collapse-header').text('I\'m a new header!');
</script>

To keep all the styling of the widget, the easiest way is to destroy the widget , change the heading text and then re-initialize the widget: 为了保持小部件的所有样式,最简单的方法是销毁小部件 ,更改标题文本,然后重新初始化小部件:

<div data-role="collapsible" id="collapse">
   <h3 id="collapse-header">I'm a header</h3>
   <p>I'm the collapsible content</p>
</div>

$("#collapse").collapsible( "destroy" );
$('#collapse-header').text('I\'m a new header!');
$("#collapse").collapsible();

DEMO 演示

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

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