解决jQuery插件tipswindown与hintbox冲突

作者:工具松鼠

时间:2026-10-25

浏览:526

本文介绍如何解决jQuery插件tipswindown与hintbox在使用ajaxStart时产生的冲突。由于两个插件共享jQuery.ajaxSettings中的全局xhr对象,导致事件触发混乱。通过修改jquery.hintbox-1.3.js中针对#windown-content的处理逻辑,将ajaxStart改为直接设置HTML,可避免冲突。

文章目录

问题背景与现象

使用场景如下:



在页面a.aspx中,点击按钮弹出模拟层,该层内包含一个名为“监督人”的输入文本框,此处调用hintbox以实现即时查询功能。


问题背景与现象

直接调用时会出现如下问题:

问题背景与现象

代码修改方案

随后定位到jquery.hintbox-1.3.js文件,发现其中存在如下代码行:

代码如下:

 
$("#windown-content").ajaxStart(function(){
$(this).html("<img src='"+templateSrc+"/images/loading.gif' class='loading' />");
}); 

将其修改为:


代码如下:

 
$("#windown-content").html("<img src='"+templateSrc+"/images/loading.gif' class='loading' />");

冲突原因分析

原因在于,即使在不同情形下触发ajax事件,这两个插件仍共用同一对象,即jQuery.js中定义的:


代码如下:

 
ajaxSetup: function( settings ) {
jQuery.extend( jQuery.ajaxSettings, settings );
}, 
ajaxSettings: {
url: location.href,
global: true,
type: "GET",
contentType: "application/x-www-form-urlencoded",
processData: true,
async: true,
/* 
timeout: 0,
data: null,
username: null,
password: null,
*/ 
// Create the request object; Microsoft failed to properly
// implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
// This function can be overriden by calling jQuery.ajaxSetup
xhr:function(){
return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}, 
accepts: {
xml: "application/xml, text/xml",
html: "text/html",
script: "text/javascript, application/javascript",
json: "application/json, text/javascript",
text: "text/plain",
_default: "*/*"
} 
}, 

这样在当前窗体中就会是只有一个xhr对象存在,因为在hintbox中的ajax事件会触发tipswindown的ajaxStart事件。



可下载修改过后的tipswindown,点击下载。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章