调用jquery函数和传递变量
不是一个JS男人 - 所以任何人都可以解释为什么这不起作用?我想从简单的AJAX脚本中调用一个脚本中的预定义函数 - 我只是希望它通过AJAX将div内容带入页面时调用qtip功能。调用jquery函数和传递变量
感谢, ^ h
脚本1 - AJAX装载机
if(myHttpRequest.readyState==4)
data.innerHTML = myHttpRequest.responseText;
qtip_me('.div_to_act_on');
}
脚本2 - 主jQuery脚本
$(document).ready(function() {
function qtip_me(a) {
$(a).each(function() {
$(this).qtip({
content: {'showme'},
position: {corner: {tooltip: 'bottomLeft', target: 'topRight'}},
style: {
width: 300,
padding: 5,
background: '#A2D959',
color: 'black',
textAlign: 'center',
border: {
width: 7,
radius: 5,
color: '#A2D959'
},
tip: 'bottomLeft',
name: 'dark' // Inherit the rest of the attributes from the preset dark style
}
});
});
};
});
可能是这样定义的函数不在范围内。 尝试定义,像这样的功能:
qtip_me = function(a) {}
这样,这将是在范围内。
或者如其他答案所述,请勿在文档中定义准备好的功能。
@tharkun:“jQuery范围”令人讨厌。哪有这回事。另外,你的语法错了。但基本上你的想法*是正确的,所以我删除了我的DV。 – 2009-11-29 13:56:17
@tharkun:你再次用“jQuery范围”废话。 -1 – 2009-11-29 13:57:45
为什么如此激进@cf?你的语言? – markus 2009-11-29 14:01:15
你不需要巢您qtip_me()的内部函数$(document).ready块。这是一个通用的函数调用,而不是依赖完全加载的文档。这是因为假设您之前的AJAX请求是在文档加载后发生的(可能是错误的假设)。
它是在词法范围内声明qtip_me函数,函数不能从外部访问它的封闭函数。
摆脱$(document).ready(function(){'和相应的结束'});''。你只是定义一个函数,你不需要它。 – 2009-11-29 13:47:11
尽管存在争论,干杯家伙 - “范围”绝对是它) – MrFidge 2009-12-01 11:17:09