面板上的 addEventListener

addEventListener on Panel

我有一个用例,我需要以编程方式 add/remove 与面板关联的 onClick 事件。

我尝试了以下解决方案,但收到 cijCell.addEventListener is not a function 错误。

function cij_enabled(){
  var cijCell = app.pages.Home.descendants.cellFour;
  var index = cijCell.styles.indexOf('disabled-card');

  if (Report.riskOfLoss === 'High') {
    cijCell.styles.splice(index, 1);
    cijCell.addEventListener("click", function() {
      app.popups.Customer.visible = true;      
    });
  } else {
    if (index === -1){
      cijCell.styles.push('disabled-card'); 
      cijCell.removeEventListener("click", function() {
      app.popups.Customer.visible = true;      
    });
    }
  }
}

我怎样才能达到预期的结果?是否可以通过应用程序制造商以这种方式添加事件监听器?

你绝对可以做到,而且你几乎做对了。您唯一需要了解的是 appmaker 小部件不是本机 html 元素,因此出现错误:

cijCell.addEventListener is not a function

幸运的是,AppMaker 有一种获取与小部件相关联的本机 html 元素的方法。您需要使用 getElement() 方法,然后您可以使用 add/remove 事件侦听器方法。因此,您应该将代码从 cijCell.addEventListener... 更改为 cijCell.getElement().addEventListener...

参考:https://developers.google.com/appmaker/scripting/api/widgets#Panel