【问题标题】:jquery IE8/9/10 Bind to click not workingjquery IE8/9/10 绑定点击不起作用
【发布时间】:2018-12-01 03:56:03
【问题描述】:

面对 IE 和 jquery 的问题。该代码可在所有其他浏览器中运行,但在 IE 中使用时会中断。相当简单的实现。但我是 javascript 新手。

console.log('hi ie');

jQuery(document).ready(function () {
    setTimeout(function () {
        jQuery(".controlApply").on("click", function (event) {
            pollVisibility();
            console.log('after poll');
        });
    }, 1000);
});
//This method checks is a specific div is shown. Dirty way to check if a report is being processed
function pollVisibility() {
    console.log('poll');
    if (microstrategy.bones.rwb_viewer.objectID == '7647F4F611E2B39B923E0080EF058C78') {
        if (!jQuery('#divWaitBox').attr('style')) {
            console.log('divWaitBox');
            //wait did not appear
            microstrategy.getViewerBone().commands.exec('refresh');
        } else if (jQuery('#divWaitBox').attr('style').indexOf('hidden') != -1) {
            console.log('hidden');
            microstrategy.getViewerBone().commands.exec('refresh');
        } else {
            console.log('other');
            setTimeout(pollVisibility, 800);
        }
    } else {}
}

console.log 永远不会被调用,但是 document.ready 似乎可以在 IE 中工作

文档类型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

如果它错了,我不能改变它。这是 MicroStrategy BI 应用程序的扩展。

【问题讨论】:

  • 愚蠢的问题,但是你有打开控制台吗?否则 console.log 将无法工作。确保在打开控制台后重新加载页面。
  • 控制台没有错误?
  • 你不是在怪癖模式,是吗?不要忘记您的&lt;!DOCTYPE html&gt;
  • 是的,我在打开控制台进行测试。它在 js 文件的顶部记录了我的 'console.log('ieucks') 就好了......
  • @RocketHazmat:.bind() 未被弃用。只是建议使用.on()

标签: javascript jquery internet-explorer internet-explorer-8


【解决方案1】:

根据您的 jQuery 版本,您的问题可能是 .bind(),应该像这样使用 .on() 执行

HTML

<div class="class" style="height:40px; width:40px; background-color:#ff0000;"></div>

jQuery

$(document).ready(function () {
    $(".class").on("click", function (event) {
        console.log('We clicked!');
        poll(); // <-- wtf does this do exactly??
    });
});

【讨论】:

  • 不,任何支持.on 的版本也将支持.bind。 ;-) 首选使用 .on,但不推荐使用 .bind()
  • @cookiemonster 它还没有被删除吗?
  • @cookiemonster 我想我应该更频繁地阅读文档As of jQuery 1.7, the .on() method is the 首选 method
  • 是的,我目前还没有听说过任何弃用或删除它的计划。使用.on() 的建议仍然很好。
  • 嘿 OP,poll(); 是做什么的?
【解决方案2】:

这个 javascript 的问题是它试图改变一个使用 ajax 框架构建的页面。它是 MicroStrategy BI 应用程序。如果我在文档准备好后放置一个断点,则页面上不会呈现任何内容。

此页面上的解决方案帮助我到达这里,它应该涵盖 if/ff/chrome

jQuery( document ).ready(function() {
    setTimeout(test, 1000);
      jQuery(".controlApply").on("click", function (event) {
          pollVisibility();
      });
});



function test(){
       jQuery('.mstrTransform').on('click', '.controlApply', function(){
           pollVisibility();
       });
}

【讨论】:

    【解决方案3】:

    我刚刚在 IE 11 中遇到了同样的问题。我试图将 jQuery append() 信息添加到 div 元素。在运行控制台时,该操作间歇性地起作用。如果控制台关闭了,什么也没有发生,就好像脚本被完全忽略了一样。这包括一个警告声明。

    通过注释掉 console.log 语句,IE 11 的行为符合预期(仍然感觉不正确)。我在 MS Edge 和 Chrome 中使用 console.logs 运行了相同的脚本,一切都按预期运行。我了解到的是,console.log 是罪魁祸首。这是我们的代码之间的共同点,我正在开发 IE 11,但问题仍然存在。 :(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      相关资源
      最近更新 更多