【问题标题】:Java Script Not working in Internet ExplorerJavascript 在 Internet Explorer 中不起作用
【发布时间】:2013-03-26 23:18:03
【问题描述】:

使用 jQuery 为网站创建了一个复选框功能,这些功能在 Chrome、Firefox、Safari、Opera 等所有其他浏览器上都可以正常工作。但它不适用于 IE 所有版本。这是我的代码:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

    <ul id="filters" style="list-style:none; margin-top:75px; line-height:30px; ">

    <li>

        <input type="checkbox" value="outdoor" id="outdoor" />

        <label for="filter-category">Outdoor</label>

    </li>

    <li>

        <input type="checkbox" value="remote_monitor" id="remote_monitor" />

        <label for="filter-category">Remote Monitor</label>

    </li>


     <li>

        <input type="checkbox" value="battery" id="battery" />

        <label for="filter-category">Battery Operated</label>

    </li>

</ul>
<div style="width:850px; height:148px; clear:both; margin-top:80px;">

<div  class="category outdoor " style=" float:left;  ">Rocco</div>

<div  class="category remote_monitor camera" style="float:left;margin-top:-2px; margin-left:10px;">Borocco</div>

<div  class="category battery" style="float:left; margin-top:-2px;margin-left:10px;">Sylva</div>

<div  class="category battery outdoor " style="float:left; margin-top:-2px;margin-left:10px;">Novesto</div>

<script>
$('input').change (function() {

        var selector = $('input:checkbox').map(function(){ 

            return this.checked ? '.' + this.id : ''; 


        }).get().join('');


        console.log(selector);  



        var all = $('div[class^="category"]');

        if(selector.length)

          all.hide().filter(selector).show()

        else all.hide();

    });
</script>

任何人都可以帮忙!

【问题讨论】:

  • 我看到你已经标记了这个问题console.log...它是否与这个问题有关:stackoverflow.com/questions/7742781/…
  • @Spudley 链接的内容。 IE 不知道console 是什么,除非开发工具打开。
  • 这个可以试试吗? if(typeof console !== 'undefined') console.log(selector)
  • @Jashwant Spudley 链接到的问题包含到 console.log 的垫片。
  • 谢谢我刚刚删除了 console.log(selector);现在它在 IE 和所有其他浏览器中运行良好。谢谢大家。

标签: javascript jquery internet-explorer checkbox console.log


【解决方案1】:

console.log() 在开发工具窗口打开之前无法在 IE 中工作。

对此的简短回答就是不要在代码中使用console.log(),除非您正在积极进行测试。如果您正在测试,无论如何您都将打开开发工具,因此代码将起作用。如果您不进行测试,请删除 console.log();它没有任何作用。

更详细的答案可以在这里找到:Why does JavaScript only work after opening developer tools in IE once?

【讨论】:

    【解决方案2】:

    您的代码在 Internet Explorer 中运行良好。我相信,就像在 cmets 中已经指出的那样 - 您没有在 Internet Explorer 中打开控制台 (f12)。您需要打开开发人员工具才能使 console.log 工作 - 否则会导致错误(并且您的 javascript 的执行将停止)

    其中一个问题:

    console.log(selector);  
    

    Your code

    【讨论】:

    • 完全正确。这都是因为console.log,所以我只是将其删除。在 IE 中工作正常。非常感谢。
    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多