在输入时显示文字 [英] Show text while typing
问题描述
I thought this one was a simple one but I wasn't able to find anything out there, except one post here on STO.
问题是代码不起作用.我创建了一个小提琴,以便您自己查看.
Problem is the code doesn't work. I created a fiddle so you can see it for yourself.
这是小提琴中的代码:
$('#someTextBox').keyup(function() {
$('#target').html(this.val());
});
但是,我的HTML与示例有所不同:
However, my HTML is a bit different than the example:
<textarea name="comment-box" id="comment-box" class="required"></textarea>
...
<p id="comment-preview"></p>
我需要帮助的是一种显示"comment-preview"
容器上textarea
上键入内容的方式.
All I need help with is a way to display what's being typed on the textarea
on the "comment-preview"
container.
在此方面为我提供指导的任何帮助都将倍受赞赏.
Any help guiding me on this one is greatly appreciated.
推荐答案
将this.val()
更改为$(this).val()
演示:: http://jsfiddle.net/FjNzS/1/
.val
是jQuery函数,可以从jQuery对象访问.在处理程序内部,this
是DOM对象,因此您需要用$()
包装它以使其成为jQuery对象.
.val
is a jQuery function and can be accessed from jQuery object. Inside the handler, this
is DOM object and so you need to wrap it with $()
to make it a jQuery object.
这篇关于在输入时显示文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!