加载后未触发Javascript代码
我正在尝试开发一个交互式网站,但在添加和执行文件时遇到了问题。因此,我在首页中有以下代码:加载后未触发Javascript代码,javascript,jquery,post,Javascript,Jquery,Post,我正在尝试开发一个交互式网站,但在添加和执行文件时遇到了问题。因此,我在首页中有以下代码: $(document).ready(function(){ $('.info').live("click",function() { if ($('#country span.selected').text().length == 0) alert("A company should be selected before conti
$(document).ready(function(){
$('.info').live("click",function()
{
if ($('#country span.selected').text().length == 0)
alert("A company should be selected before continue!");
else {
$('.centered').hide();
$('.waitimg').show();
$.post('get_data.php',
{
type :$(this).attr("id"),
company : $('#country span.selected').text(),
company_id : $('#country').attr("value"),
country : $('#scountry span.selected').text(),
industry: $('#industry span.selected').text()
}, function(response)
{
//$('#resultarea').fadeOut();
setTimeout("finishAjax('resultarea','"+escape(response)+"')", 400);
}
);
}
return false;
});
});
以及回调:
function finishAjax(id, response) {
$('#waitimg').hide();
$.getScript("js/script.js");
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
它应该执行js/script.js中的脚本,但它没有。使用Firebug,我可以看到文件已加载,但script.js中的简单警报(“hello world”)未被触发。为什么?
setTimeout
要求函数引用,而不是字符串作为第一个参数。替换:
setTimeout("finishAjax('resultarea','"+escape(response)+"')", 400);
与:
你已经完成了@StackOverfolow:我不是通灵者,所以我不知道你的页面是什么样子,或者其他什么代码可能会导致问题。因此,我们可以看到您尝试了什么响应正常,我的问题是script.js中的代码根本没有执行。尝试向
getScript
调用添加一个成功回调,记录一些事情。。。但是,请设置一个提琴-并检查您的控制台,像“尝试,不起作用”这样的回答并没有说明代码失败的地方。当我用Firebug测试时,脚本工作得很好,文件已加载。@StackOverfolow:只是有点迂腐,但您站点的JS中有许多行,如setTimeout(“scanHTML5Videos()”之类, 2000);代码>,请将其替换为设置超时(ScanHTML5视频,2000)代码>,小提琴不正确(检查左侧:framework mootools应该是jQuery,因此代码在那里不起作用。另外:我看到您使用的是1.7版,所以不要使用.live
方法,而是使用.on
方法
setTimeout(function()
{
return finishAjax.apply(this, ['resultarea', escape(response)]);//call as though finishAjax was callback --> preserve context
//or, if you don't care about the context:
return finishAjax('resultarea', escape(response));
}, 400);