在输入时显示文字

编程入门 行业动态 更新时间:2024-10-21 16:14:08
本文介绍了在输入时显示文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我认为这是一个简单的例子,但是除了在此处输入文本帖子.

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()

演示:: jsfiddle/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.

更多推荐

在输入时显示文字

本文发布于:2023-11-11 22:47:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!