仅对一个脚本使用jQuery(不干扰其他JS代码)

时间:2017-01-06 10:37:20

标签: javascript jquery html

我遇到了问题,我编写了一些jQuery脚本:

$(document).ready(function () {
  $('#sshin').keyup(function () {
    var query = $(this).val();
    if(query != '') {
      $.ajax({
        url: "search.php",
        method: "POST",
        data: {query: query},
        success: function (data) {
          $('#sshout').fadeIn();
          $('#sshout').html(data);
        }
      });
    }
  });
  $(document).on('click', 'article', function () {
    var text = $('#sshin').val();
    text = text.substring(0, text.lastIndexOf(',') + 1);
    text += $(this).text();

    var uniq = text.match(/\b\w+\b/g).filter(function (el, idx, a) {
      return idx === a.lastIndexOf(el);
    });

    text = uniq.join(',');

    $('#sshin').val(text);
    $('#sshout').fadeOut();
  });
});

当然在index.html文件中我包含了jQuery库和我的JS代码。当我测试它而没有包含在另一个文件中时,一切都工作得非常好。

我的问题是,我的index.html被另一个文件包含(我根本无法改变),并且显然会干扰其他JS,例如JQuery代码..

是否可以只使用我为此脚本包含的jQuery库,或者您还有其他想法吗?

如果您需要任何其他信息,请与我们联系。

错误输出:

slim.js:3 
Uncaught TypeError: $(...).live is not a function
at chooseLinkContainer (slim.js:3)
at HTMLDocument.<anonymous> (slim.js:3)
at k (slim.js:2)
at Object.fireWith [as resolveWith] (slim.js:2)
at Function.ready (slim.js:2)
at HTMLDocument.D (slim.js:2)

jquery-1.10.2.js:8672 
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

piwik.js:58 
Uncaught Error: A siteId must be given to add a new tracker
at K.addTracker (piwik.js:58)
at Object.addTracker (piwik.js:66)
at piwik.js:69
at piwik.js:69
at eval (eval at <anonymous> (jquery-1.10.2.js:612), <anonymous>:3:10)
at eval (<anonymous>)
at jquery-1.10.2.js:612
at Function.globalEval (jquery-1.10.2.js:613)
at init.domManip (jquery-1.10.2.js:6281)
at init.append (jquery-1.10.2.js:6047)
删除我的Jquery参考后的

错误消息:

slim.js:2 Synchronous XMLHttpRequest on the main thread is deprecated
because of its detrimental effects to the end user's experience. 
For more help, check https://xhr.spec.whatwg.org/.




piwik.js:58 
Uncaught Error: A siteId must be given to add a new tracker
at K.addTracker (piwik.js:58)
at Object.addTracker (piwik.js:66)
at piwik.js:69
at piwik.js:69
at eval (eval at <anonymous> (slim.js:2), <anonymous>:3:10)
at eval (<anonymous>)
at slim.js:2
at Function.globalEval (slim.js:2)
at HTMLScriptElement.<anonymous> (slim.js:2)
at Function.each (slim.js:2)

1 个答案:

答案 0 :(得分:2)

问题是你使用的是两个版本的jQuery,一个旧版本的live方法在最新版本中删除了。你的jQuery正在覆盖前一个,但问题是如果jQuery存在那么真的需要再次添加它吗?

几种可能的解决方案:

1.只需删除第二个jQuery源代码(使用live方法保留旧版本)并在jQuery附加到页面后添加脚本,好地方位于html标记的末尾。

2.如果解决方案1不起作用,请使用 noConflict 添加版本(在将其附加到页面后):

    var myJq = $.noConflict(true);
    function ( $ ){
      //your code
    }(myJq);

第二个解决方案可以在本地函数范围内使用 $ 符号,而不会与其他文件冲突,在全局范围内,最新的jQuery将在myJq变量中。

有关在一个页面上使用多个jQuery的更多有用信息 - Can I use multiple versions of jQuery on the same page?

3.删除解决方案1中的第二个jQuery,并在window.onload回调中添加您的代码 - 在将所有脚本加载到页面后,它将是肯定的。

window.onload = function() {

  //code
};
相关问题