如何在输入标签中使用javasript显示实时输入?(How to show real time input using javascript in input tag?)

编程入门 行业动态 更新时间:2024-10-26 22:27:20
如何在输入标签中使用javasript显示实时输入?(How to show real time input using javascript in input tag?)

我怎样才能使这个脚本运行通过替换textarea标记与输入标记? 我在我的页面中使用了输入标签,并希望添加此代码,但此代码使用了textarea。 如果我更改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> 
  
 

How can I make this script run by replacing textarea tag with input tag? I used input tag in my page and wanna add this code but this code used textarea. if I change textarea to input the code doesn't work.

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> 
  
 

最满意答案

尝试这个:-

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

或者,使用document.getElementsByClassName

方法.getElementsByClassName()返回一个类似数组的对象,所以你必须访问第一个元素(如果有的话)。

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

或者,您也可以使用.querySelector()方法:

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

Try this:-

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

Or, Using document.getElementsByClassName

The method .getElementsByClassName() returns an array-like object of elements.so you have to access first element (if there is any).

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

or, you could also use the .querySelector() method:

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

更多推荐

本文发布于:2023-08-07 21:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实时   标签   如何在   javasript   show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!