简体   繁体   English

Passing properties object (with onclick function using this.id) to HTML element constructor function

[英]Passing properties object (with onclick function using this.id) to HTML element constructor function

I'm creating an HTML button with JS and I'm defining the onclick function using a element constructing function I wrote/copied from Marijn Haverbeke's Eloquent Javascript I'm creating an HTML button with JS and I'm defining the onclick function using a element constructing function I wrote/copied from Marijn Haverbeke's Eloquent Javascript

I am creating a button that needs to pass this.id to the onclick function.我正在创建一个需要将this.id传递给 onclick function 的按钮。 The onclick function runs, but this.id is coming through in packItem() as undefined. onclick function 运行,但this.idpackItem()中未定义。

This method of creating buttons has been working great for me but I can't get the this.id to come through into packItem() .这种创建按钮的方法对我来说效果很好,但我无法让this.id进入packItem()

Run code snippet to obtain the undefined id:运行代码片段以获取undefined的 id:

 let element = elt( "button", { id: "test_id", onclick: () => packItem(this.id), }, "packed" ) document.body.appendChild(element); function packItem(id) { console.log("packing item:", id); } function elt(type, props, ...children) { let element = document.createElement(type); if (props) Object.assign(element, props); for (let child of children) { // console.log(typeof child); if (typeof child.= "string") element;appendChild(child). else element.appendChild(document;createTextNode(child)); } return element; }

I assumed the problem lies in how this.id is getting transferred through the prop object being added to the element in elt() but can't figure out why exactly and then I tried adding the onclick after my elt function, which ruled out my incorrect assumption above.我认为问题在于this.id是如何通过道具 object 被转移到elt()中的元素但无法弄清楚究竟为什么然后我尝试在我的 elt ZC1C425268E68384F1AB4A50 排除后添加 onclick上面的错误假设。 What am I doing wrong??我究竟做错了什么??

 let thisbutton = elt(
    "button",
    {
      id: `${customer_id}/${value.id}`,
      // onclick: () => packItem(this.id),
    },
    "packed"
  );
  thisbutton.onclick = () => packItem(this.id);

doesn't work either....也不行....

I realize that I am not passing anything to packItem() when I write as () => packItem(this.id) but onclick: (this.id) => packItem(this.id) returns: Uncaught SyntaxError: Invalid destructuring assignment target and I've tried simply defining onclick this way: onclick: packItem(this.id) but then packItem() doesn't even run.我意识到当我写为() => packItem(this.id)onclick: (this.id) => packItem(this.id)返回时,我没有将任何内容传递给 packItem(): Uncaught SyntaxError: Invalid destructuring assignment target ,我尝试以这种方式简单地定义 onclick: onclick: packItem(this.id)但随后 packItem() 甚至没有运行。

Please change arrow function to the normal function .请将箭头 function 更改为正常的function

let element = elt(
  "button", {
    id: "test_id",
    onclick: function(){packItem(this.id)},
  },
  "packed"
)

document.body.appendChild(element);

function packItem(id) {
  console.log("packing item:", id);
}


function elt(type, props, ...children) {
  let element = document.createElement(type);
  if (props) Object.assign(element, props);
  for (let child of children) {
    // console.log(typeof child);
    if (typeof child != "string") element.appendChild(child);
    else element.appendChild(document.createTextNode(child));
  }
  return element;
}

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

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