简体   繁体   English

此代码在IE中均适用于所有其他浏览器

[英]This code works in all other browsers except in IE

This works fine in all browsers except IE can anyone explain why so I can fix it. 这在所有浏览器中都可以正常工作,但IE谁都可以解释原因,因此我可以修复它。 I am displaying the index of a javascript object based on the index of the selected index of my dropdown list 我正在根据下拉列表中所选索引的索引显示javascript对象的索引

 $(document).ready(function () {
var pdata = [{ Name: "Apples", Price: 1.99 },{ Name: "Bananas", Price: 2.45 } ];

    $('#produceTMPL').tmpl(pdata).appendTo('#produceList');

      $(document).ready(function () {


      $('#add1').click(function () {
        var selected = $('#produceList option:selected').index();

        item = pdata[selected];

        console.log(selected);
        $('#cart').append('<p>' + item.Name + ', ' + item.Price + '</p>');




    });  
    });

HTML: HTML:

     <div>
  <select id="produceList">
  <option>make a selection</option>
  </select>

item is a protected property of the window object in IE. item是IE中窗口对象的受保护属性。 Just rename your variable, or declare it properly (using var ) in your function. 只需重命名您的变量,或在函数中正确声明它(使用var )。

console.log throws an error in IE, so to be on the safer side DO NOT use console.log with out checking if browser supports it or not. console.log在IE中引发错误,因此为了安全起见,请勿在未检查浏览器是否支持console.log的情况下使用console.log。

try{
    if(console && console.log) console.log('message')   
}catch(e){}

OR 要么

 if ('undefined' !== typeof console){console.log(message); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM