javascript - 在 HTML 元素 onkeyup 中返回输出

标签 javascript jquery html

我正在使用 html 编辑器,但这部分给我带来了一个问题,是否有办法存档它?当我在文本字段中输入内容时,它将在 div 元素中显示输出。

<script type="javascript/text">
    function ColorText(){
        T = Rep(document.getElementById("text").value);
        document.getElementById("wcode").innerHTML=T;
        setTimeout("ColorText()",10);
    }
</script>

这是 HTML 部分

<input type="text" id="text" onkeypress="ColorText()"/>
<div type="text" id="wcode"></div>

最佳答案

更优雅的解决方案,无需 setTimeout:

function Rep(value){
  //Do your thing...
  return value;
}

var wcode = document.getElementById("wcode");
var text = document.getElementById("text");

text.addEventListener("input", function(){ 
  wcode.innerHTML = Rep(this.value);
});
<input type="text" id="text"/>
<div id="wcode"></div>

关于javascript - 在 HTML 元素 onkeyup 中返回输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34965294/

相关文章:

javascript - 在js中捕获自定义异常

javascript - iOS 9.2 上的 Buzz 与 AudioContext(网络音频 API)

javascript - jQuery Validate 不会在提交时触发?

javascript - 对背景图像的淡入效果

javascript - 图像 Angular 根据鼠标位置变化

jquery - 使用 jquery 选择多个实例

javascript - 根据 slider 图像的色调更改菜单颜色

javascript - 如何在不使用选择器刷新网页的情况下导航到其他网站或网页?

javascript - 选项卡的滑动效果第 2 部分

javascript - 创建 jquery 菜单 - json 响应