Javascript 函数在ajax加载的内容中也可以在删除后运行

Javascript 函数在ajax加载的内容中也可以在删除后运行,javascript,jquery,function,Javascript,Jquery,Function,我的html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><script src="js/jquery.js"></script> &l

我的html页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script src="js/jquery.js"></script>
<script>$(function(){
$("button:first").click(function(){
$("#aja").load("ajax.html");});
$("button:last").click(function(){
$("#aja").remove();});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<button>Load page</button><button id="rem">Remove ajax content</button>
<div id="aja"></div>
</body>
</html>

$(函数(){
$(“按钮:第一”)。单击(函数(){
$(“#aja”).load(“ajax.html”);};
$(“按钮:最后”)。单击(函数(){
$(“#aja”).remove();});
});
无标题文件
加载页面删除ajax内容
和ajax.html代码

<body>
<script>$(function(){
cool();});
function cool(){
$("button:last").after("Hello");
setTimeout("cool()",10000);}
</script>
</body>

$(函数(){
酷();});
函数cool(){
$(“按钮:最后”)。在(“你好”)之后;
setTimeout(“cool()”,10000);}

现在,当我加载ajax.html时,它的cool()函数会运行并不断向最后一个按钮添加hello被删除的函数将继续运行。但我希望在删除ajax内容后停止酷函数

检查是否存在
\aja
块可能会有帮助:

function cool() {
    if ($("#aja").length == 0) return;

    $("button:last").after("Hello");
    setTimeout("cool()", 10000);
}

您想使用清除超时


@Webtecher这是我想到的第一个想法。然而,这个问题有点奇怪:)如果你有任何其他想法,那么你的plz更新将被接受answer@Webtecher我怀疑我是否能给出更好的主意。这个应该很好用。