DHTML - 使用输入字段更改页面上的文本

发布于 2025-01-02 01:06:55 字数 546 浏览 2 评论 0原文

我需要创建一个代码,以便当用户在输入字段中键入内容时将示例文本更改为用户定义的值(类似于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-01-09 01:06:55

你需要在函数中有这样的东西:

function changetext(id){
   var info = document.getElementById('input').value();
   id.innerHTML = info;
}

This js is not完全正确。我强烈建议您开始使用 JavaScript 库,例如 jQuery。这使得这成为一项卑微的任务。

编辑:

jQuery 将在 IE8 中正常工作。在 jQuery 中,您不需要将 js 附加到您的输入中。代码看起来像这样。

$('#input').click(function(){
    $('#preview).html(this.val());
});

它更加干净,并且 html 中没有 js。

You need to have something like this in the function:

function changetext(id){
   var info = document.getElementById('input').value();
   id.innerHTML = info;
}

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.

$('#input').click(function(){
    $('#preview).html(this.val());
});

It is a lot cleaner and doesnt have js in the html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文