简体   繁体   English

JS-如何向带有参数的div中添加onclick事件?

[英]JS - How to add an onclick event to a div with parameter?

I know how to add an onclick event to a div without parameter : 我知道如何将不带参数的onclick事件添加到div中:

newDiv.onclick = selectUnit;

function selectUnit() {}

But I could not make it work with parameters : 但是我无法使其与参数一起使用:

function appendUnit(nb) {
    var newDiv = document.createElement("div");

    newDiv.id = "unit" + nb;
    newDiv.onclick = selectUnit(this.id); // This throws me undefined
    document.getElementById("unitsList").appendChild(newDiv);
}

function selectUnit(id) {
    console.debug(id);
}

How can I do that ? 我怎样才能做到这一点 ?

You'll need an anonymous function for that, as there is no way to pass arguments to a referenced function 您将需要一个匿名函数,因为无法将参数传递给引用的函数

function appendUnit() {
    var newDiv = document.createElement("div");

    newDiv.onclick = function() {
        selectUnit(this.id);
    }

    document.getElementById("unitsList").appendChild(newDiv);
}

function selectUnit(id) {
    console.debug(id);
}

but note that the value of this will keep, so you can do 但要注意的价值this将继续,所以你可以做

function appendUnit() {
    var newDiv = document.createElement("div");

    newDiv.onclick = selectUnit;

    document.getElementById("unitsList").appendChild(newDiv);
}

function selectUnit() {
    console.debug( this.id ); // this is still the same here
}

With that line of code 有了那行代码

newDiv.onclick = selectUnit(this.id);

you just call the function, get its result and store it to the onclick handler. 您只需调用该函数,获取其结果并将其存储到onclick处理程序即可。

A function with no return defined inside returns undefined. 内部没有定义返回的函数将返回未定义。 The this.id will refer to the this element you currently have at your scope which may be the window object. this.id将引用您当前在范围内具有的this元素, this元素可能是window对象。

When the onclick happens, somewhere chrome will call this function 当点击发生时,Chrome会在某个地方调用此函数

DOMElement.onclick(EventObject);

And with your line it will be something like this 和你的线将是这样的

(undefined)(this.id);

which leads to errors 导致错误

All you have to do is to set onclick with a method 您要做的就是使用方法设置onclick

newDiv.onclick = selectUnit;

And chrome will call this chrome会称之为

DOMElement.onclick(EventObject);

Having DOMElement.onclick == selectUnit we can assume the upper line of code is similar to this: 有了DOMElement.onclick == selectUnit我们可以假设DOMElement.onclick == selectUnit与此类似:

selectUnit(EventObject);

Then on your selectUnit function you must know how to access the id . 然后,在selectUnit函数上,您必须知道如何访问id You can see at https://developer.mozilla.org/en-US/docs/Web/API/Event what you can do with it. 您可以在https://developer.mozilla.org/zh-CN/docs/Web/API/Event上看到可以使用的功能。 So the new selectUnit function will be: 因此,新的selectUnit函数将是:

function selectUnit(event) {
    var id = event.target.id;
    console.debug(id);
}

You never set the id in your example code: 您永远不会在示例代码中设置ID:

function appendUnit() {
    var newDiv = document.createElement("div");
    newDiv.id = "somevalue";

    [...]
    newDiv.onclick = "selectUnit(this.id);" // This throws me undefined
    document.getElementById("unitsList").appendChild(newDiv);
}

function selectUnit(id) {
    console.debug(id);
}

Try this 尝试这个

function appendUnit() {
    var newDiv = document.createElement("div");

    [...]
    newDiv.onclick = function(){
        selectUnit(newDiv.id); // This throws me undefined
    }
    document.getElementById("unitsList").appendChild(newDiv);
}

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

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