函数调用的参数错误无效,特别是IE和FF

时间:2011-06-10 20:53:41

标签: javascript internet-explorer firefox

我正在使用http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript

中的这个小javascript'工具箱'

到目前为止,我的代码适用于Firefox和Chrome,但不适用于IE。

* 编辑 * Firefox中的Nore ..

我在调用getCSSRule“工具箱”函数的地方收到了无效参数错误。 http://d.pr/Ai5D和DevTools http://d.pr/btFU

以下是相关代码:

function myModal() {
  if (window.location.href.indexOf("#fr") != -1) {
    getCSSRule("#MB_content").style.backgroundImage = "url('/mailer/img/bgimg-fr.jpg') !important";
    Modalbox.show('mailer/myModal-fr.html', {title: 'Nouveau', width: 1000}); return false;
  } else {
    Modalbox.show('mailer/myModal-en.html', {title: 'Sign-Up', width: 1000}); return false;
  }
}

function thankYou() {
  getCSSRule("#MB_content").style.backgroundImage = "none !important";
  getCSSRule("#modalWrap").style.minHeight = "0 !important";
}

您可以在此处查看任何其他文件https://github.com/jwmann/Modal-Sign-up/tree/master/mailer

1 个答案:

答案 0 :(得分:2)

getCSSRule的文档说它返回样式对象。 您希望它返回一个DOM对象。

所以:

getCSSRule("#MB_content").style.backgroundImage = "url('/mailer/img/bgimg-fr.jpg') !important";

应该是

getCSSRule("#MB_content").backgroundImage = "url('/mailer/img/bgimg-fr.jpg') !important";

但是,文档还提供了返回DOM对象的用法示例。混乱!

第2部分: 通过更多测试我发现:

  • 如果文档上没有样式标记或外部样式表则失败
  • 如果您的对象#MB_content没有定义CSS规则,并且您使用getCSSRule则会产生错误。在这种情况下,请使用addCSSRule。