覆盖JavaScript中的jQuery Core函数-getJSON [英] Overriding a jQuery Core function in javascript - getJSON

查看:61
本文介绍了覆盖JavaScript中的jQuery Core函数-getJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖jQuery中的getJSON函数以提供其他参数,然后调用原始的getJSON.我不太熟悉JavaScript,因此已将其与其他类似示例一起使用.我看到的大多数示例都覆盖了jQuery.fn命名空间中的函数的行为.有人可以通过告诉我我做错了什么来帮助我吗?

I am trying to override the getJSON function in jQuery to provide additional params and then call the original getJSON. I am fairly unfamiliar with JavaScript and have scraped this together from other similar examples. Most of the examples I have seen override the behavior from a function in the jQuery.fn namespace. Could someone help me by telling me what I am doing wrong?

<html>
<head>
<script type="text/javascript" src="./js/jquery-1.6.4.min.js"></script>
<script type="text/javascript">

(function($){    
    var _old = $.getJSON;
    $.getJSON = function(){
        alert("Calling overridden getJSON");    
        return _old.apply(this,arguments);
    };
})(jQuery);

$(document).ready(function() {
    try {
        $(this).getJSON('ajax/test.json', function(data) {
            var items = [];
            alert('done');
        });
    }
    catch (err)
    {
        alert(err.message);
    }
});
</script>
</head>
<body>
</body>
</html>

推荐答案

您需要致电$.getJSON,而不是$(foo).getJSON;

You need to call $.getJSON, not $(foo).getJSON;

$.getJSON('ajax/test.json', function(data) {
    var items = [];
    alert('done');
});

这篇关于覆盖JavaScript中的jQuery Core函数-getJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆