简体   繁体   English

如果数据属性(HTML)=== javascript 中的输入文本,如何更改已写入输入的每个字母的 javascript 颜色?

[英]How can I change color in javascript for each letter which has been written into an input if data attribute(HTML) === input text in javascript?

can you tell me please how can I change color (green is right, red is wrong) for each letter which has been written(in right order) into an input column if data attribute === input text in Javascript?如果数据属性 === Javascript 中的输入文本您能告诉我如何更改已写入(按正确顺序)输入列中的每个字母的颜色(绿色是正确的,红色是错误的)

I mean if data="word" input value ="word"我的意思是如果 data="word" 输入值="word" 例子

When I write w - letter color is green else letter color red.当我写 w -字母颜色为绿色时,字母颜色为红色。

wo -stay green else red;
wor - green else red;
word -green else red;

My code is here:我的代码在这里:

let all = document.querySelectorAll('#input_Text')
all.forEach(element => element.addEventListener('input',(e)=>{
    if(e.target.value === element.getAttribute('data-verb')){
    element.style.border = "5px solid green"
    
    }else{element.style.border = "5px solid red"};
    }));

Thx a lot多谢

You can use Document.querySelectorAll() to get all input elements 'input[data-verb]' and attach an input event to set style border if the element's value String.prototype.startsWith() the entered text:如果元素的值String.prototype.startsWith()输入的文本,您可以使用Document.querySelectorAll()获取所有输入元素'input[data-verb]'并附加input事件以设置样式边框:

 document .querySelectorAll('input[data-verb]') .forEach(el => el.addEventListener('input', ({ target: { value }}) => { const color = el.getAttribute('data-verb').startsWith(value) ? 'green' : 'red' el.style.border = `5px solid ${color}` }))
 <input type="text" data-verb="word">

  1. I've changed your id to class assuming there will be multiple inputs.假设会有多个输入,我已将您的id更改为class
  2. Also, use String.prototype.startsWith to check if data-verb starts with current input value.此外,使用String.prototype.startsWith检查data-verb是否以当前输入值开头。
  3. Use element.style.border to set the element border.使用element.style.border设置元素边框。

 let all = document.querySelectorAll('.input_Text') all.forEach(element => element.addEventListener('input',(e)=>{ var data = element.getAttribute('data-verb').toString(); var value = e.target.value; if (e.target.value == ""){ element.style.border = "none"; } else if (data.startsWith(value)){ element.style.border = "5px solid green"; } else{ element.style.border = "5px solid red"; } }));
 <input type='text' class='input_Text' data-verb="word">

Assign the same class to each input that you want to check a word for, then loop through the contents of querySelectorAll , adding an event listener to each of them.为要检查单词的每个输入分配相同的类,然后遍历querySelectorAll的内容,为每个输入添加一个事件侦听器。

Then you can use includes to see if the contents of what's typed matches the form's data attribute:然后您可以使用includes来查看键入的内容是否与表单的数据属性匹配:

 let words = document.querySelectorAll('.word'); words.forEach((word) => { word.addEventListener('input', () => { if (word.dataset.verb.includes(word.value)) { word.style.border = '5px solid green'; } else { word.style.border = '5px solid red'; } }); });
 <input type="text" data-verb="apple" class="word"><br /> <input type="text" data-verb="banana" class="word">

暂无
暂无

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

相关问题 如何检查更改时的文本输入字符串是否具有大写字母 Javascript - How to check if a text input string on change has an Uppercase letter Javascript 如何检查更改时的文本输入字符串是否具有小写字母 Javascript - How to check if a text input string on change has a Lowercase letter Javascript 如何更改输入文本值已在javascript中设置 - how to change input text value has been set in javascript 我如何转移已输入的值 <input number> 对象转换成javascript而不刷新页面? - How can i transfer value which has been typed in <input number> object into javascript without page refreshing? 如何对用户以 html 形式输入的数据应用字符串化? - How can i apply stringify on the data which has been input by the user in an html form? 如何使用Javascript来判断浏览器是否更改了HTML输入字段? - How can I use Javascript to tell whether an HTML input field has been changed by the browser? 如何将 php 数据设置为 javascript 日期并更改输入文本的颜色 - How to set php data to javascript date and change color of the input text html5文件输入-如何使用javascript更改文件文本的颜色? - html5 file input - how do I change the color of the file text using javascript? Javascript-在写下文本区域之前更改输入字母 - Javascript - Change input letter before written down textarea Javascript-数组输入的每个字母 - Javascript - Each letter of an input to an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM