Jquery更改< p>文本编程 [英] Jquery change <p> text programmatically

查看:474
本文介绍了Jquery更改< p>文本编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:解决方案是将此添加到个人资料页面而不是性别页面。

The solution was to add this to the profile page instead of the gender page.

$('#profile').live( 'pageinit',function(event){
$('p#pTest').text(localStorage.getItem('gender'));

});

我在列表视图中有一个带有som文本的段落,我想要在clikcing save之后以编程方式从另一页面进行更改。

I have a paragraph with som text in a listview that I want to change programatically from another page after clikcing save.

编辑:这是我在profile.html中的列表视图。当您点击该项目时,您将进入另一个可以保存性别的页面。我想将此列表视图中的段落更改为从其他页面更改的性别。

This is my listview in profile.html. When you click on the item you get to another page where you can save your gender. I want to change the paragraph in this listview to the gender that was changed from the other page.

<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代码(在单独的文件中):

The gender html page is just basic stuff with two radio buttons and a save button. Here is my javascript code(in a seperate file):

$('#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');

I have tried this with javascript: $('p#pTest').text('test');

但是文字没有改变。 (我知道保存按钮有效)。这可能吗?

The text does not change however. (I know that the save button works). Is this possible?

推荐答案

请尝试以下操作,请注意当用户刷新页面时,值再次为男性,数据应存储在数据库中。

Try the following, note that when user refreshes the page, the value is "Male" again, data should be stored on database.

<p id="pTest">Male</p>
<button>change</button>

<script>
$('button').click(function(){
     $('#pTest').text('test')
})
</script>

http:// jsfiddle.net/CA5Cs/

这篇关于Jquery更改&lt; p&gt;文本编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆