使用输入标签实时更改 HTML

发布于 2024-12-29 08:14:53 字数 337 浏览 3 评论 0原文

如何使用 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 技术交流群。

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

发布评论

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

评论(6

等风来 2025-01-05 08:14:53

您可以使用 jQuery

$('input').keyup(function(){
    var a = $(this).val();
    $('#example').text(a); 
});

示例来执行此操作: http://jsfiddle.net/5TnGT/

You can do this with jQuery

$('input').keyup(function(){
    var a = $(this).val();
    $('#example').text(a); 
});

Example: http://jsfiddle.net/5TnGT/

老街孤人 2025-01-05 08:14:53

除了前面的答案中所述之外,还有许多其他方法可以更改内容。聆听所有内容并实时更新。此示例需要 jQuery 支持较新的 .on() 事件处理。还可以使用 .bind().live() 以及适当的语法。

$(document).ready(function() {
    $(document).on('keyup propertychange input paste', 'input', function() {
        $('#example').text($(this).val());
    }); 
});

第二个 $(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.

$(document).ready(function() {
    $(document).on('keyup propertychange input paste', 'input', function() {
        $('#example').text($(this).val());
    }); 
});

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/

耶耶耶 2025-01-05 08:14:53

是的,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)

离笑几人歌 2025-01-05 08:14:53

简单的 JavaScript 解决方案(如果你在其他地方不太喜欢的话,你不需要任何复杂的库):

<input type="text" onkeypress="document.getElementById('example').innerHTML=this.value;" name="whatever" />
<div id="example"></div>

Plain JavaScript solution (you won't need any sophisticated lib if you don't get too fancy elsewhere):

<input type="text" onkeypress="document.getElementById('example').innerHTML=this.value;" name="whatever" />
<div id="example"></div>
踏雪无痕 2025-01-05 08:14:53

您可以绑定到 keyup 事件。然后将 div 内容设置为输入的内容。

http://jsfiddle.net/wYqgc/

var input = document.getElementsByTagName('input')[0];
var div = document.getElementById('example');
input.onkeyup = function() {
    div.innerHTML = this.value;
};

You can bind to the keyup event. Then set the div contents to that of the input.

http://jsfiddle.net/wYqgc/

var input = document.getElementsByTagName('input')[0];
var div = document.getElementById('example');
input.onkeyup = function() {
    div.innerHTML = this.value;
};
御守 2025-01-05 08:14:53

您可以从以下开始:

input.onkeyup = function () {
    output.innerHTML = this.value;
};

现场演示: http://jsfiddle.net/P4jS9/

You can start with this:

input.onkeyup = function () {
    output.innerHTML = this.value;
};

Live demo: http://jsfiddle.net/P4jS9/

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