编辑:解决方案是将其添加到个人资料页面而不是性别页面。
$('#profile').live( 'pageinit',function(event){
$('p#pTest').text(localStorage.getItem('gender'));
});
我在 ListView 中有一个带有 som 文本的段落,我想在点击保存后从另一个页面以编程方式更改它。
编辑:这是我在 profile.html 中的 ListView 。当您单击该项目时,您会进入另一个页面,您可以在其中保存您的性别。我想将此 ListView 中的段落更改为从其他页面更改的性别。
<ul data-role="listview" >
<li><a href="gender.html">
<img src="images/gender2.jpg" />
<h3>Gender</h3>
<p id="pTest">Male</p>
</a></li> </ul>
性别 html 页面只是带有两个单选按钮和一个保存按钮的基本内容。 这是我的 javascript 代码(在一个单独的文件中):
$('#gender').live('pageinit', function(event) {
var gender = localStorage.getItem('gender');
var boolMale = true;
if (gender == "female") boolMale = false;
$('#radio-choice-male').attr("checked",boolMale).checkboxradio("refresh");
$('#radio-choice-female').attr("checked",!boolMale).checkboxradio("refresh");
$('#saveGenderButton').click(function() {
if ($('#radio-choice-male').is(':checked'))
localStorage.setItem('gender', "male");
else localStorage.setItem('gender', "female");
$('#pTest').html('test'); //does not work
//$('p#pTest').text('test'); //does not work
//$('#pTest').text('test'); //does not work
$.mobile.changePage("profile.html");
});
});
我已经用 javascript 试过了:$('p#pTest').text('test');
但是文本没有改变。 (我知道保存按钮有效)。这可能吗?
最佳答案
试试下面的,注意当用户刷新页面时,值又是“Male”,数据应该存储在数据库中。
$('button').click(function(){
$('#pTest').text('test')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<p id="pTest">Male</p>
<button>change</button>
关于javascript - Jquery 以编程方式更改 <p> 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486010/