在输入时显示文字 [英] Show text while typing

查看:99
本文介绍了在输入时显示文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个简单的例子,但是除了

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屋!

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