Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅对一个脚本使用jQuery(不干扰其他JS代码)_Javascript_Jquery_Html - Fatal编程技术网

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

Javascript 仅对一个脚本使用jQuery(不干扰其他JS代码),javascript,jquery,html,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}, succe

我有一个问题,我写了一些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)
slim.js:3
未捕获的TypeError:$(…)。live不是函数
在chooseLinkContainer上(slim.js:3)
在HTMLDocument。(slim.js:3)
在k(slim.js:2)
在Object.fireWith[as resolveWith](slim.js:2)
at Function.ready(slim.js:2)
在HTMLDocument.D(slim.js:2)
jquery-1.10.2.js:8672
主线程上的同步XMLHttpRequest不推荐使用,因为它会对最终用户的体验产生有害影响。如需更多帮助,请查看https://xhr.spec.whatwg.org/.
piwik.js:58
未捕获错误:必须提供siteId才能添加新跟踪器
在K.addTracker(piwik.js:58)
在Object.addTracker(piwik.js:66)
在piwik.js:69
在piwik.js:69
评估时(评估时(jquery-1.10.2.js:612),:3:10)
评估时()
在jquery-1.10.2.js:612
在Function.globalEval(jquery-1.10.2.js:613)
在init.domManip(jquery-1.10.2.js:6281)
在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)
slim.js:2主线程上的同步XMLHttpRequest已被弃用
因为它会对最终用户的体验产生不利影响。
如需更多帮助,请查看https://xhr.spec.whatwg.org/.
piwik.js:58
未捕获错误:必须提供siteId才能添加新跟踪器
在K.addTracker(piwik.js:58)
在Object.addTracker(piwik.js:66)
在piwik.js:69
在piwik.js:69
评估时(评估时(slim.js:2),:3:10)
评估时()
在slim.js:2
位于Function.globalEval(slim.js:2)
在HTMLScriptElement。(slim.js:2)
在Function.each(slim.js:2)

问题在于,您使用的是两个版本的jQuery,一个旧版本带有
live
方法,该方法在最新版本中被删除。您的jQuery正在覆盖上一个jQuery,但问题是,如果jQuery存在,那么确实需要再次添加它吗

几种可能的解决办法:

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


2.如果解决方案1不起作用,请使用noConflict作为添加版本(添加到第页之后):

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

关于在一个页面上使用多个jQuery的更多有用信息-


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

window.onload = function() {

  //code
};

我建议您用Javascript编写,如果您不依赖jquery,这种事情就不会发生。是否可能在另一个文件中包含
index.html
时出现错误,或者至少有一些关于JS和jQuery哪些部分重叠的指示?这还取决于如何将
index.html
文件包含在另一个文件中file@Pineda我将这些错误编辑到我的帖子中,你说的“包含在另一个文件中”是什么意思?通过脚本异步加载?很抱歉,这一切都不起作用。//我仍然会收到错误消息,是的。.在我删除第二个JQuery库链接后,仍然会收到错误(更少但仍然是错误)主线程上的此警告消息
Synchronous XMLHttpRequest已被弃用,因为它会对最终用户的体验造成有害影响
如果是这样,则仅意味着如果您将删除所做的一切,也会存在错误,因此这一定是不同的问题-与您的代码无关。如果我删除脚本(附在上面),错误不在那里,因此它必须与我的代码有关“在我删除第二个JQuery库链接之后”-您删除了添加的链接或其中的链接?您需要删除您添加的链接。
window.onload = function() {

  //code
};