您应该在身体内部使用容器,而不要将代码直接放在身体中。
即。添加在体内的div标签,这将是集装箱
<div class="container"></div>
然后在JS调用
$('.container').html(response).show('slow');
这样的内容加载在容器中,而不是直接在体内将取代所有该页面的内容包括你在那里的JS。
另外,当使用Ajax调用时,我认为它使得代码更清晰,将响应传递给其他函数进行处理。这样你将有更小的功能来调试和更容易理解的代码。
$.ajax({
url: 'ajax/test.html',
success: function(data) {
//Here you pass the response to the other function
processSuccess(data);
},
fail: function(data) {
//Here you pass the response to the other function
processFail(data);
}
});
function processSuccess(response) {
//Print the response in the console (ie. Firebug console)
//if console is available
if(console) {
console.log(response);
}
};
function processFail(response) {
//Print the response in the console (ie. Firebug console)
//if console is available
if(console) {
console.log(response);
}
};
当然,在命名空间内的所有内容都会使它更好。
如果您的脚本在body标签内,html()方法会清除所有内容,请考虑将数据附加到另一个元素而不是body元素。 – undefined
'我有以下的javascript''$ .ajax({type:... url:... data:... bla bla ...'。那怎么执行? – Nope
'javascript'是什么意思?消失“?它是否加载了页面? –