DHTML - 使用输入字段更改页面上的文本
我需要创建一个代码,以便当用户在输入字段中键入内容时将示例文本更改为用户定义的值(类似于在 Stack Overflow 上编写问题时的预览字段)。
这需要在不使用 HTML5 或 Flash 的情况下实现,因为用户将运行 IE8,并非所有用户都会安装 Flash 插件。
因此,我开始通过查看 DHTML 来达到预期的效果。目前,当用户在输入字段中键入时,我可以更改示例文本,但只能更改为预定义值(下面代码中的“示例”),我应该如何编辑此代码以显示用户定义的值?
JS
function changetext(id)
{
id.innerHTML="Example";
}
HTML
<form>
Content:<input type="text" id="input" onkeyup="changetext(preview)" />
</form>
<p id="preview">No content found</p>
I need to create a code to change an example text to a user-defined value when the user types in an input field (Similar to the preview field when writing a question on Stack Overflow).
This needs to be achieved without the use of HTML5 or Flash as the users will be running IE8, not all will have Flash plug-ins installed.
As such I have started by looking at DHTML to achieve the desired effect. Currently I can change the example text when a user types in the input field but only to a pre-defined value ("Example" in the code below), how should I edit this code to display the user-defined value?
JS
function changetext(id)
{
id.innerHTML="Example";
}
HTML
<form>
Content:<input type="text" id="input" onkeyup="changetext(preview)" />
</form>
<p id="preview">No content found</p>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要在函数中有这样的东西:
This js is not完全正确。我强烈建议您开始使用 JavaScript 库,例如 jQuery。这使得这成为一项卑微的任务。
编辑:
jQuery 将在 IE8 中正常工作。在 jQuery 中,您不需要将 js 附加到您的输入中。代码看起来像这样。
它更加干净,并且 html 中没有 js。
You need to have something like this in the function:
This js is not fully correct. I would highly recommend you start using a javascript library like jQuery. It makes this a menial task.
Edited:
jQuery will work in IE8 just fine. in jQuery you will not need to attach js to your input. The code would look like this.
It is a lot cleaner and doesnt have js in the html.