0

我正在为 PHP 和 JavaScript 中的 Magento 开发一个模块。

JavaScript 部分使用 Magento 附带的默认 PrototypeJS 库。这是我第一次使用 Prototype。

无论如何,下面的这部分代码会更改我的模块插入某些页面的 Div

这就是我现在遇到的问题,当此代码运行在它尝试修改的页面中没有 Div 的页面上时。

请帮我修改,以便它只能在找到具有类名的 Div 时运行此代码.nam-modal-body

// Set Modal Div CSS Width
$$('.nam-modal-body')[0].setStyle({
   width: this.options.modalWidth + "px",
  'margin-left': -(this.options.modalWidth / 2) + "px"
});
4

2 回答 2

0
var myDiv = document.getElementsByClassName('nam-modal-body');
if (myDiv[0].length > 0) {
    $$('.nam-modal-body')[0].setStyle({
       width: this.options.modalWidth + "px",
      'margin-left': -(this.options.modalWidth / 2) + "px"
    });
}

或者

if ($$('.nam-modal-body').length > 0) {
    $$('.nam-modal-body')[0].setStyle({
       width: this.options.modalWidth + "px",
      'margin-left': -(this.options.modalWidth / 2) + "px"
    });
}
于 2013-08-10T19:02:04.603 回答
0

为了摆脱错误,我最终不得不使用这个......

if (typeof($$('.nam-modal-body')[0]) != 'undefined') {
    $$('.nam-modal-body')[0].setStyle({
       width: this.options.modalWidth + "px",
      'margin-left': -(this.options.modalWidth / 2) + "px"
    });
}
于 2013-08-10T21:51:19.243 回答