使用jQuery动态更改段落文本? [英] Change paragraph text dynamically with jQuery?
问题描述
我想将输入的信息输入文本字段,并将其显示在页面其他位置的段落中.基本上,下面就是我键入的内容(去提问,然后开始在主文本框中键入内容,您将明白我的意思).
I want to take the information typed into a text field and display it in a paragraph elsewhere on the page. Basically, exactly what is happening below as I'm type this (go to post a question and start typing in the main text box and you'll see what I mean).
任何想法如何做到这一点?该页面上有很多JavaScript,我找不到他们是怎么做到的.
Any idea how to do this? The page has so much JavaScript on it I can't find how they did it.
推荐答案
这是您的html外观:
Here is how your html should look like:
<textarea id="txt" style="width:600px; height:200px;"></textarea>
<p id="preview"></p>
和jQuery:
$(function(){
$('#txt').keyup(function(){
$('#preview').text($(this).val());
});
});
这将在其keyup
事件上获取textarea
的值,然后将该段落的文本更改为textarea
$(this).val()
的文本(text()
方法).
This grabs the value of textarea
on its keyup
event and later the paragraph's text is changed (text()
method) with that of textarea
$(this).val()
.
这篇关于使用jQuery动态更改段落文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!