Javascript Jquery 以编程方式更改 <p> 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10486010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery change <p> text programmatically
提问by oivindth
EDIT: 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'));
});
});
I have a paragraph with som text in a listview that I want to change programatically from another page after clikcing save.
我在列表视图中有一段带有一些文本的段落,我想在单击保存后以编程方式从另一个页面更改。
EDIT: 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.
编辑:这是我在 profile.html 中的列表视图。当您点击该项目时,您会进入另一个页面,您可以在其中保存您的性别。我想将此列表视图中的段落更改为从其他页面更改的性别。
<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>
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):
性别 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");
});
});
I have tried this with javascript: $('p#pTest').text('test');
我用javascript试过这个: $('p#pTest').text('test');
The text does not change however. (I know that the save button works). Is this possible?
然而,文本并没有改变。(我知道保存按钮有效)。这可能吗?
回答by undefined
Try the following, note that when user refreshes the page, the value is "Male" again, data should be stored on database.
尝试以下操作,注意当用户刷新页面时,该值再次为“Male”,数据应存储在数据库中。
<p id="pTest">Male</p>
<button>change</button>
<script>
$('button').click(function(){
$('#pTest').text('test')
})
</script>
回答by Sinetheta
"saving" is something wholly different from changing paragraph content with jquery.
“保存”与使用 jquery 更改段落内容完全不同。
If you need to save changes you will have to write them to your server somehow (likely form submission along with all the security and input sanitizing that entails). If you have information that is saved on the server then you are no longer changing the content of a paragraph, you are drawing a paragraph with dynamic content (either from a database or a file which your server altered when you did the "saving").
如果您需要保存更改,您将不得不以某种方式将它们写入您的服务器(可能是表单提交以及所需的所有安全性和输入清理)。如果您有保存在服务器上的信息,那么您不再更改段落的内容,而是绘制具有动态内容的段落(来自数据库或您的服务器在“保存”时更改的文件) .
Judging by your question, this is a topic on which you will have to do MUCH more research.
从你的问题来看,这是一个你必须做更多研究的主题。
Input page (input.html):
输入页面(input.html):
<form action="/saveMyParagraph.php">
<input name="pContent" type="text"></input>
</form>
Saving page (saveMyParagraph.php) and Ouput page (output.php):
保存页面(saveMyParagraph.php)和输出页面(output.php):
回答by Juan Leung
It seems you have the click event wrapped around a custom event name "pageinit", are you sure you're triggered the event before you click the button?
您似乎将点击事件包裹在自定义事件名称“pageinit”周围,您确定在单击按钮之前触发了该事件吗?
something like this:
像这样:
$("#gender").trigger("pageinit");