使用输入标签实时更改 HTML
如何使用 input
标签实时更改(更改、添加等)HTML/文本?与在 Stack Overflow 上提问时的预览界面非常相似,只是去掉了代码编码。它必须很简单。
例如,
<input type="text" name="whatever" />
<div id="example"></div>
在上述 input
标签中输入的任何文本都会实时添加到 #example
中。 也许涉及 innerHTML
和 JavaScript?
How can I can I alter (change, add, whatever) HTML/text real-time using the input
tag? Very similar to the preview interface when asking a question on Stack Overflow minus the code encoding. It just has to be simple.
For example,
<input type="text" name="whatever" />
<div id="example"></div>
Whatever text is entered in the above input
tag is added to #example
in real-time.
Something involving innerHTML
and JavaScript perhaps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 jQuery
示例来执行此操作: http://jsfiddle.net/5TnGT/
You can do this with jQuery
Example: http://jsfiddle.net/5TnGT/
除了前面的答案中所述之外,还有许多其他方法可以更改内容。聆听所有内容并实时更新。此示例需要 jQuery 支持较新的
.on()
事件处理。还可以使用.bind()
或.live()
以及适当的语法。第二个 $(document) 可以根据页面其余部分的标记变得更加具体。
另请参阅:http://jsfiddle.net/DccuN/2/
There are many other ways to change content than described in the previous answers. Listen for all of them and update realtime. Requires jQuery supporting the newer
.on()
event handling for this example. Can also use.bind()
or.live()
with appropriate syntax.The second $(document) can be made more specific depending on the markup of the rest of your page.
See also: http://jsfiddle.net/DccuN/2/
是的,javascript 会做到这一点。查看按键。然后,正如您所说的innerHTML或jQuery使事情变得更容易 .append 或 .html 或 .text
(太慢了)
Yes javascript will do this. Have a look at on key up. Then either innerHTML as you say or jQuery makes things a bit easier with .append or .html or .text
(Damn too slow)
简单的 JavaScript 解决方案(如果你在其他地方不太喜欢的话,你不需要任何复杂的库):
Plain JavaScript solution (you won't need any sophisticated lib if you don't get too fancy elsewhere):
您可以绑定到 keyup 事件。然后将 div 内容设置为输入的内容。
http://jsfiddle.net/wYqgc/
You can bind to the keyup event. Then set the div contents to that of the input.
http://jsfiddle.net/wYqgc/
您可以从以下开始:
现场演示: http://jsfiddle.net/P4jS9/
You can start with this:
Live demo: http://jsfiddle.net/P4jS9/