简体   繁体   English

运行功能onclick。 单击所有按钮

[英]Running functions onclick. All buttons being clicked

I have many buttons that have class="clearSelect" . 我有许多具有class="clearSelect"按钮。 I want these buttons the execute a function onclick. 我希望这些按钮执行onclick函数。 I am new to javascript and not quite sure why this is occurring but I think my functions are being executed instead of only executing onclick 我是javascript新手,不太确定为什么会这样,但是我认为我的功能正在执行,而不是仅执行onclick

The code below is what is calling all my other functions causing every button to be clicked. 下面的代码正在调用我的所有其他函数,从而导致每个按钮都被单击。

code: 码:

var buttons = document.getElementsByClassName("clearSelect");  // objects with class="clearSelect"

for (var i = 0; i < buttons.length; i++) {
    var button = buttons[i];
    // button.addEventListener("onclick", clearOrSelectAll(button.id));
    button.onclick = clearOrSelectAll(button.id);
}

These are the functions being called: 这些是被调用的函数:

function clearOrSelectAll(btn) {
    var cleartab = clearButtonSet[btn];
    var selecttab = selectButtonSet[btn];
    // console.log("clicked!");
    if (cleartab != null) {
        getOSList(cleartab, false);
    } else {
        getOSList(selecttab, true);
    }
}

function getOSList(tabVal, fate) {
    var configTab = document.getElementById(tabVal);
    var browserList = configTab.getElementsByClassName("browser_list");
    // var idObjs = browserList.getElementsByTagName("li");
    for (var h = 0; h < browserList.length; h++) {
        var idObjs = browserList[h].getElementsByTagName("li");
        // console.log(h);
        // console.log(idObjs);
        // select all
        if (fate) {
            for (var i = 0; i < idObjs.length; i++) {
                if (configs[idObjs[i].id] == null) {
                    idObjs[i].className = "selected";
                    configs[idObjs[i].id] = config_dictionary[idObjs[i].id];
                }
            }
        // clear all
        } else {
            for (var j = 0; j < idObjs.length; j++) {
                if (configs[idObjs[j].id] == null) {
                    idObjs[j].className = "unselected";
                    delete configs[idObjs[j].id];
                }
            }
        }
    }
}

@Christopher was very close, but button.id should be this.id . @Christopher非常接近,但是button.id应该是this.id

button.onclick = function() {
  clearOrSelectAll(this.id);
}

The reason button.id doesn't work can be demonstrated with this code: 可以通过以下代码演示button.id不起作用的原因:

 var buttons= document.getElementsByTagName('button'); for (var i = 0; i < buttons.length; i++) { var button = buttons[i]; button.onclick = function() { alert(button.id); } } 
 <button id="B1">Button 1</button> <button id="B2">Button 2</button> <button id="B3">Button 3</button> 

Each button returns "B3," because that's the last object that the variable button is assigned to. 每个按钮都返回“ B3”,因为这是变量button分配给的最后一个对象。

In your for loop when you attach the event to all of the buttons, you are calling the clearOrSelectAll function. 在将事件附加到所有按钮上的for循环中,您正在调用clearOrSelectAll函数。 You probably want to wrap it in an anonymous function to make sure it's only called when the event is fired. 您可能希望将其包装在匿名函数中,以确保仅在事件触发时才调用它。

// Non-ideal solution: see edit
button.onclick = function() {
    clearOrSelectAll(button.id);
}

EDIT: It has been pointed out that the 'this' context variable will point to the element in question when an event handler is attached by means of the onclick property, or the addEventListener method. 编辑:已经指出,当通过onclick属性或addEventListener方法附加事件处理程序时,“ this”上下文变量将指向有问题的元素。 As such it would probably be cleaner (and easier to read) if you were to reference that instead of using 'button' as a closure and count on javascript engines to not optimize your loop too heavily (as that would mess with the value of 'button' at the time that the event is called. 因此,如果您要引用它而不是使用“按钮”作为闭包并依靠javascript引擎不要过度优化循环(可能会弄混“”的值,则可能会更干净(更易于阅读)。按钮”在事件被调用时。

button.onclick = function() {
    clearOrSelectAll(this.id);
};

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

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