jquery - 未捕获的类型错误 : Cannot call method 'noConflict' of undefined

标签 jquery salesforce javascript visualforce

我试图在 SalesForce 中使用 javascript 和 jquery 创建一个小部件,我遇到了一个错误:Uncaught TypeError: Cannot call method 'noConflict' of undefined

下面是我正在使用的代码

(function() {

// Localize jQuery variable
var $j;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "https://ap1.salesforce.com/resource/go/jquery.min.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    $j = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    $j = window.jQuery.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    $j(document).ready(function($) { 
        /******* Load CSS *******/
       var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "style.css" 
        });
        css_link.appendTo('head');          

        /******* Load HTML *******/
        var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
        $.getJSON(jsonp_url, function(data) {
          $('#example-widget-container').html("This data comes from another server: " + data.html);
        });

    });
}

})(); // We call our anonymous function immediately

在浏览器控制台中,ERROR 指向 scriptLoadHandler 函数的第一行。

最佳答案

如下声明本地 jquery 变量:

//Localize jQuery variable
var $j = jQuery.noConflict();

关于jquery - 未捕获的类型错误 : Cannot call method 'noConflict' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17315604/

相关文章:

javascript - 选项卡委托(delegate)不工作

javascript - 当通过 javascript/jquery 更改值时,多个选择不会更新

javascript - 如何使用 javascript 检查 URL 是否可用

javascript - jQuery 和输入[type=file][multiple=multiple] 值

javascript - Facebook 如何将此格式化文本放入 JavaScript 控制台?

jquery - 如何在没有 Mac 的情况下测试网站是否支持 Retina?

data-structures - 业务分析师的 Salesforce 表关系

testing - 通过 SOAP API 插入 ApexTestQueueItem 记录时启用调试日志记录

javascript - 在许多 promise 链之间共享全局已解决的 promise 会导致内存泄漏或性能下降吗?

salesforce - Apex AddError 到 SObject 字段