简体   繁体   English

Javascript停止输入多个类名

[英]Javascript stops working on entering multiple class name

I am a simple problem here with JavaScript. 我在这里使用JavaScript是一个简单的问题。 I have this JS code which I use to highlight rows in a price table. 我有这个JS代码,用于突出显示价格表中的行。 The issue is that if I used multiple class names at a time it stops working. 问题是,如果我一次使用多个类名,它就会停止工作。 For example: 例如:

<div class="el1 someclass">Hover does not work in this</div>

<div class="el1">Hover does work in this</div>

The JS: JS:

var classes = ["el1", "el2", "el3", "el4", "el5", "el6", "el7", "el8", "el9","el10","el11","el12", "el13","el14","el15","el16","el17","el18","el19","el20","el21","el22", "el23", "el24","el25" ]; //list of your classes
var elms = {};
var n = {}, nclasses = classes.length;
function changeColor(classname, color) {
    var curN = n[classname];
    for(var i = 0; i < curN; i ++) {
        elms[classname][i].style.backgroundColor = color;
    }
}
for(var k = 0; k < nclasses; k ++) {
    var curClass = classes[k];
    elms[curClass] = document.getElementsByClassName(curClass);
    n[curClass] = elms[curClass].length;
    var curN = n[curClass];
    for(var i = 0; i < curN; i ++) {
        elms[curClass][i].onmouseover = function() {
            changeColor(this.className, "#dbdbdb");
        };
        elms[curClass][i].onmouseout = function() {
            changeColor(this.className, "transparent");
        };
    }
};

Can somebody help me out please, I'm a newbie in JS. 有人可以帮帮我,我是JS的新手。

Thanks .. 谢谢 ..

 var classes = ["el1", "el2", "el3", "el4", "el5", "el6", "el7", "el8", "el9","el10","el11","el12", "el13","el14","el15","el16","el17","el18","el19","el20","el21","el22", "el23", "el24","el25" ]; //list of your classes var elms = {}; var n = {}, nclasses = classes.length; function changeColor(classname, color) { var curN = n[classname]; for(var i = 0; i < curN; i ++) { elms[classname][i].style.backgroundColor = color; } } for(var k = 0; k < nclasses; k ++) { var curClass = classes[k]; elms[curClass] = document.getElementsByClassName(curClass); n[curClass] = elms[curClass].length; var curN = n[curClass]; for(var i = 0; i < curN; i ++) { elms[curClass][i].onmouseover = function() { changeColor(this.className.split(' ')[0], "#dbdbdb"); }; elms[curClass][i].onmouseout = function() { changeColor(this.className.split(' ')[0], "transparent"); }; } }; 
 <div class="el1 someclass">Hover does not work in this</div> <br><br><br><br><br><br> <div class="el1">Hover does work in this</div> 

The problem was with the 问题在于

function changeColor(classname, color) {
    var curN = n[classname];
    for(var i = 0; i < curN; i ++) {
        elms[classname][i].style.backgroundColor = color;
    }
}

Here you're assigning hover to each element where this reference would 在这里,您将悬停分配给此引用所在的每个元素

>>            changeColor(this, "#dbdbdb");

pass the only one element and will change the color 传递唯一的一个元素,将改变颜色

The className property on a dom element returns the complete className; dom元素上的className属性返回完整的className; that is, if some element has multiple classes (eg <div class="herp derp"></div> ), then className = "herp derp" . 也就是说,如果某个元素有多个类(例如<div class="herp derp"></div> ),则className = "herp derp"

If you want to call changeColor on each of these multiple classes, try something like this: 如果要在这些多个类中调用changeColor ,请尝试以下方法:

// first, change the order of the arguments on the changeColor function so we can pre-apply the color argument
function changeColor(color, classname) {
    var curN = n[classname];
    for(var i = 0; i < curN; i ++) {
        elms[classname][i].style.backgroundColor = color;
    }
}

Now, we can change how we call changeColor 现在,我们可以改变调用changeColor的方式

// now, where you previously called changeColor, do something like this
this.className.split(" ").forEach(changeColor.bind(null, "transparent"));
// we now will call changeColor on all of the classes of this with the appropriate color ("transparent" or "#dbdbdb")

Check out the fiddle I created. 看看我创造的小提琴。 The problem is when you are trying to change color you are again passing in the class name and trying to select the element again while you already have the reference to the element where you are changing the background color. 问题是,当您尝试更改颜色时,您再次传递类名并尝试再次选择元素,同时您已经具有对要更改背景颜色的元素的引用。 So pass on the element directly and change the background. 所以直接传递元素并改变背景。

http://jsfiddle.net/5eLhsqxv/ http://jsfiddle.net/5eLhsqxv/

` `

var n = {}, nclasses = classes.length;
function changeColor(elm, color) {
        elm.style.backgroundColor = color;
}
for(var k = 0; k < nclasses; k ++) {
    var curClass = classes[k];
    elms[curClass] = document.getElementsByClassName(curClass);
    console.log(elms[curClass]);
    console.log(curClass);
    n[curClass] = elms[curClass].length;
    var curN = n[curClass];
    for(var i = 0; i < curN; i ++) {
        console.log(elms[curClass][i]);
        elms[curClass][i].onmouseover = function() {
            changeColor(this, "#dbdbdb");
        };
        elms[curClass][i].onmouseout = function() {
            changeColor(this, "transparent");
        };
    }
};

` `

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

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