javascript WTF,幻影函数调用
我试图让“帮助”工具提示在您单击某个类的标签时出现,在您单击“离开”时消失。什么也没有出现。我可以在firebug中设置断点并查看“加载”工具提示,然后当ajax返回正确的工具提示更新时,removeTooltip将被调用phantomly(堆栈跟踪在jquery中只显示F()F()。因此,工具提示设置和删除得如此之快,以至于从未见过javascript WTF,幻影函数调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试图让“帮助”工具提示在您单击某个类的标签时出现,在您单击“离开”时消失。什么也没有出现。我可以在firebug中设置断点并查看“加载”工具提示,然后当ajax返回正确的工具提示更新时,removeTooltip将被调用phantomly(堆栈跟踪在jquery中只显示F()F()。因此,工具提示设置和删除得如此之快,以至于从未见过 HelpText.removeTooltip = function() { $('#activeHelpTip').remove(); $('bo
HelpText.removeTooltip = function() {
$('#activeHelpTip').remove();
$('body').unbind('click', HelpText.removeTooltip);
}
HelpText.initToolTip = function(clickedElement) {
$('body').click(HelpText.removeTooltip);
$(clickedElement).append('<span id="activeHelpTip" class="helpTip">Loading help...</span>');
}
HelpText.updateTooltip = function(helpString, clickedElement, methodName) {
if (helpString == null) { helpString = "Help text has not been defined for selected field"; }
$('#activeHelpTip').html(helpString);
}
$(document).ready(function() {
$('.helpText').click(function() {
var helpRequested = $(this).html();
var path = window.location.pathname;
var fullPage = path.substring(path.lastIndexOf('/') + 1);
var page = fullPage.substring(0, fullPage.indexOf('.'));
var label_helpRequested = $(this).html();
HelpText.initToolTip(this);
HelpText.getHelpText(page, label_helpRequested, this);
});
HelpText.getHelpText = function(pageNameParam, fieldNameParam, element) {
var params = { pageName: pageNameParam, fieldName: fieldNameParam };
if (this._webRequest) {
// abort the previous web service call if we
// are issuing a new one and the previous one is
// active.
this._webRequest.get_executor().abort();
this._webRequest = null;
}
// this._webRequest is a handler on the async request
this._webRequest = Sys.Net.WebServiceProxy.invoke(HelpTextServiceURL,
"GetHelpText",
false, /* use GET */
params, /* parameters to the Ajax service method - case and type sensitive */
HelpText.updateTooltip, /* success callback */
null, /* failure callback */
element); /* user context - preserved info - accessed in the success callback - in this case will contain SPAN */
}
HelpText.removeTooltip=function(){
$('#activeHelpTip')。删除();
$('body').unbind('click',HelpText.removeTooltip);
}
HelpText.initToolTip=函数(单击元素){
$('body')。单击(HelpText.removeTooltip);
$(clickedElement).append('加载帮助…');
}
HelpText.updateTooltip=函数(helpString、clickedElement、methodName){
如果(helpString==null){helpString=“尚未为所选字段定义帮助文本”;}
$('#activeHelpTip').html(helpString);
}
$(文档).ready(函数(){
$('.helpText')。单击(函数(){
var helpRequested=$(this.html();
var path=window.location.pathname;
var fullPage=path.substring(path.lastIndexOf('/')+1);
var page=fullPage.substring(0,fullPage.indexOf('.');
var label_helpRequested=$(this.html();
HelpText.initToolTip(此);
getHelpText(页面,标签_helpRequested,this);
});
HelpText.getHelpText=函数(pageNameParam、fieldNameParam、element){
var params={pageName:pageNameParam,fieldName:fieldNameParam};
如果(此._webRequest){
//如果需要,请中止上一个web服务调用
//正在发布一个新的,而上一个是
//活跃的。
此._webRequest.get_executor().abort();
这是。_webRequest=null;
}
//此.\u webRequest是异步请求上的处理程序
此._webRequest=Sys.Net.WebServiceProxy.invoke(HelpTextServiceURL,
“GetHelpText”,
false,/*使用GET*/
Ajax服务方法的params、/*参数-区分大小写和类型*/
HelpText.updateTooltip,/*成功回调*/
null,/*失败回调*/
元素);/*用户上下文-保留的信息-在成功回调中访问-在本例中将包含SPAN*/
}
您的initToolTip
函数为整个页面主体设置了一个单击处理程序,以调用removeToolTip()
。我想,当单击事件在$('.helpText')上触发时,会发生什么
,添加工具提示,然后单击事件将冒泡到body
元素,此时正在调用removeToolTip()
。您的initToolTip
函数为整个页面主体设置一个单击处理程序,以调用removeToolTip()
。我想象发生的情况是,当单击事件在$('.helpText')
上触发时,工具提示被添加,然后单击事件冒泡到主体
元素,此时removeToolTip()
正在被调用。您尚未取消显示工具提示的事件上的冒泡,您要做的第一件事是将移除处理程序附加到正文。因此,当您的init处理程序结束时,jQuery和浏览器会将该事件委派到链的上游,在那里可以看到并处理您的移除处理程序
解决方案是取消init处理程序中的冒泡。您尚未取消显示工具提示的事件上的冒泡,您要做的第一件事是将移除处理程序附加到主体。因此,当init处理程序结束时,jQuery和浏览器会将该事件委派到链的上游,在那里可以看到和处理移除处理程序
解决方案是取消init处理程序中的冒泡。我尝试使用选择器$('body:not(.helpText')。单击(helpText.removeTooltip);这并没有解决问题。这不会改变情况,除非您有
。我尝试使用选择器$('body:not(.helpText')。单击(helpText.removeTooltip);这并没有解决问题。除非您有一个
,否则情况不会改变。对于记录,我通过从处理程序返回false来实现。对于记录,我通过从处理程序返回false来实现。