gpt4 book ai didi

javascript - 像本例中那样一起使用时,.each 和 $(this) 的普通 Javascript 等价物是什么?

转载 作者:搜寻专家 更新时间:2023-11-01 04:17:18 25 4
gpt4 key购买 nike

在此示例中一起使用时,.each$(this).find 的普通 Javascript 等效项是什么?

$(document).ready(function(){

$('.rows').each(function(){
var textfield = $(this).find(".textfield");
var colorbox = $(this).find(".box");

function colorchange() {
if (textfield.val() <100 || textfield.val() == null) {

colorbox.css("background-color","red");
colorbox.html("Too Low");
}

else if (textfield.val() >300) {

colorbox.css("background-color","red");
colorbox.html("Too High");
}

else {

colorbox.css("background-color","green");
colorbox.html("Just Right");
}
}
textfield.keyup(colorchange);
}

)});

这基本上是我想要完成的 fiddle ,我知道我需要使用循环我只是不确定如何设置它。如果不需要的话,我不想仅仅为了这个简单的功能而使用 jquery

http://jsfiddle.net/8u5dj/

我删除了我已经尝试过的代码,因为它改变了颜色框的每个实例,所以我不确定我做错了什么。

最佳答案

这是用普通 javascript 做你想做的事情的方法:

http://jsfiddle.net/johnboker/6A5WS/4/

var rows = document.getElementsByClassName('rows');

for(var i = 0; i < rows.length; i++)
{
var textfield = rows[i].getElementsByClassName('textfield')[0];
var colorbox = rows[i].getElementsByClassName('box')[0];

var colorchange = function(tf, cb)
{
return function()
{
if (tf.value < 100 || tf.value == null)
{
cb.style.backgroundColor = 'red';
cb.innerText = "Too Low";
}
else if (tf.value > 300)
{
cb.style.backgroundColor = 'red';
cb.innerText = "Too High";
}
else
{
cb.style.backgroundColor = 'green';
cb.innerText = "Just Right";
}
};
}(textfield, colorbox);

textfield.onkeyup = colorchange;
}

关于javascript - 像本例中那样一起使用时,.each 和 $(this) 的普通 Javascript 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21706330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com