jQuery错误仅在IE(8)中出现 - "对象不支持..."

6
我有一个jQuery中的代码片段(AJAX)。
$.get('someScript.php?lat=' + clickedLatLng.lat() + '&lon=' + clickedLatLng.lng() + '&value=5', doSomething);

这是一个函数(在Google地图上显示图标并更改一些输入字段的值)。

function doSomething(data) {
    data = data.trim();
    data = data.split(","); 
    var stopName = data[0];
    var stopLat = data[1];
    var stopLon = data[2];

    $("#start").val(stopName);

    if (startMarker == null) {
        startMarker = new google.maps.Marker({
                            position: new google.maps.LatLng(stopLat,stopLon),
                            map: map,
                            zIndex: 2,
                            title: stopName,
                            icon: startImage 
        });
    } else {
        startMarker.setPosition(new google.maps.LatLng(stopLat,stopLon));
    }
}

但在我的情况下,除了IE浏览器之外,它在所有浏览器中都可以正常工作。我没有在IE 6/7中进行测试。它会弹出这个错误... jquery error in IE 8 我查看了jquery.js文件,发现它在这个函数中出错...
            // resolve with given context and args
            resolveWith: function( context, args ) {
                if ( !cancelled && !fired && !firing ) {
                    firing = 1;
                    try {
                        while( callbacks[ 0 ] ) {
                            callbacks.shift().apply( context, args );
                        }
                    }
                    finally {
                        fired = [ context, args ];
                        firing = 0;
                    }
                }
                return this;
            },

实际上
callbacks.shift().apply( context, args );

有人能帮忙吗?问题出在哪里?同样的问题也出现在 jquery-1.4.4.js 上。

编辑:这是我的完整代码...

    // Set some events on the context menu links
contextMenu.find('a').click( function()
{
    // fade out the menu
    contextMenu.fadeOut(75);

    // The link's href minus the #
    var action = $(this).attr('href').substr(1);

    switch (action) {
        case 'startMenu':
            $.get('someScript.php?lat=' + clickedLatLng.lat() + '&lon=' + clickedLatLng.lng() + '&radijus=5', doSomethingWithData1);
            break;

        case 'stopMenu':
            $.get('someScript.php?lat=' + clickedLatLng.lat() + '&lon=' + clickedLatLng.lng() + '&radijus=5', doSomethingWithData2);    
            break;
    }

    return false;
});

当用户在谷歌地图的上下文菜单中点击某个项目时,则执行“doSomethingWithData1”和“doSomethingWithData2”。以下是一些上下文菜单的代码。
    // Hover events for effect
contextMenu.find('a').hover( function() {
    $(this).parent().addClass('hover');
}, function() {
    $(this).parent().removeClass('hover');
});

这是关于AJAX的内容

$.ajaxSetup ({  
        cache: false  
    }); 

这是我如何包含我的 jQuery 脚本。

    <!-- Google Maps -->       
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<!-- Load Javascript / jQuery -->
<script type="text/javascript" src="js/jquery-1.5.js"></script>
<script type="text/javascript" src="js/jquery.ui.core.js"></script>
<script type="text/javascript" src="js/jquery.ui.position.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="js/jquery.ui.autocomplete.js"></script>

<link rel="stylesheet" href="js/jquery.ptTimeSelect.css" type="text/css" media="all" />

<script type="text/javascript" src="js/jqtransformplugin/jquery.jqtransform.js"></script>
<link rel="stylesheet" href="js/jqtransformplugin/jqtransform.css" type="text/css" media="all" />

<script type="text/javascript" src="js/jquery.ui.datepicker.js"></script>

如果你在 jquery.js 文件中遇到了错误,99.9999% 的情况下,这是 你的 代码出了问题。我会进一步调查。 - Dutchie432
您能提供一下在HTML标记中如何引用jQuery.js的方法吗? - David Hoerster
另外,您是在页面加载时还是在回调函数中遇到了错误?您的回调函数是否真正被调用了? - David Hoerster
我建议在setPosition回调函数的第一行设置一个断点,并逐步执行它。查看生成的data并确保它是有意义的。找出实际失败的代码行(你引用的jQuery行只是调用你的回调函数;更有可能错误在回调函数内部)。 - T.J. Crowder
2个回答

10

这就是问题所在 =/ - JavaScript中的.trim()在IE中无法正常工作

解决方案 - 在使用.trim函数之前添加以下内容。

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

别忘了接受自己的答案,这样以后来查看这个问题的人就能一眼看到解决方法。 - Malice

0
问题在于此时变量callbacks不是一个数组,因此callbacks.shift()失败了。
至于原因,可能有点多虑,但可以尝试按照以下方式编写代码。
var url = 'someScript.php?lat=' + clickedLatLng.lat() + '&lon=' + clickedLatLng.lng() + '&value=5';
$.get(url,function(data){
console.log(data);
});

//if the above worked then try this
$.get(url, doSomething);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接