简体   繁体   English

如何在输入代码中使用javascript显示实时输入?

[英]How to show real time input using javascript in input tag?

How can I make this script run by replacing textarea tag with input tag? 如何通过将textarea标记替换为input标记来使该脚本运行? I used input tag in my page and wanna add this code but this code used textarea. 我在页面中使用了输入标签,并希望添加此代码,但是此代码使用了textarea。 if I change textarea to input the code doesn't work. 如果我更改textarea来输入代码,将无法正常工作。

 function setFontText(text) { document.getElementById("Courier_new").innerHTML = text; document.getElementById("Arial_Black").innerHTML = text; } 
 <textarea id="textField0" autocomplete="off" style="font-family:'Alex Brush';" onkeyup="javascript:setFontText(this.value);" rows="2" name="textField0"></textarea> Multiple text area: <textarea id="Courier_new" class="fontTextArea2" style="font-family:Courier;" autocomplete="off" name="Courier new"></textarea> <textarea id="Arial_Black" class="fontTextArea2" style="font-family:Arial; " autocomplete="off" name="Arial Black"></textarea> 

Try this:- 尝试这个:-

function setFontText(text) {
  document.getElementById("Courier_new").value = text;
  document.getElementById("Arial_Black").value = text;
}

Or, Using document.getElementsByClassName 或者,使用document.getElementsByClassName

The method .getElementsByClassName() returns an array-like object of elements.so you have to access first element (if there is any). .getElementsByClassName()方法返回元素的类似数组的对象。因此,您必须访问第一个元素(如果有)。

var elements = document.getElementsByClassName("anyclass);
var value = elements[0].value;

alert(value); // 1

or, you could also use the .querySelector() method: 或者,您也可以使用.querySelector()方法:

var value = document.querySelector('.anyclass').value;
alert(value); // 1

Update: 更新:

Register the function: 注册功能:

function setFontText(text) {
  document.getElementById("Courier_new").innerHTML = text;
  document.getElementById("Arial_Black").innerHTML = text;
}

Call the function with a string as parameter. 以字符串作为参数调用该函数。

setFontText("TEXT");

it works fine with textarea as well. 它也适用于textarea。 Syntax would be like this. 语法就是这样。 Here for the input field in stackoverflow: 这里是stackoverflow中的输入字段:

var x = document.getElementById("wmd-input") x.innerText = "TEXTFIELDINPUT" var x = document.getElementById(“ wmd-input”)x.innerText =“ TEXTFIELDINPUT”

or 要么

x.innerHTML = "TEXTFIELDINPUT"* x.innerHTML =“ TEXTFIELDINPUT” *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM