问题
I'm trying to use this with tabs that load content with ajax into a div. I can't get it to refresh on the interval. The top part does work, however.
<script type="text/javascript">
 $(function rlAl() {
    if ($("#xicon1").hasClass("active")) {
    $("#actionlist").load("alcurrent.php");
    }
    else if ($("#xicon2").hasClass("active")) {
    alert("icon2");
    }
    else if ($("#xicon3").hasClass("active")) {
    alert("icon3");
    }
 });
 $(function() {
 setInterval(rlAl, 5000);
});
</script>
    回答1:
rlAl is undefined because it wasn't attached to the global scope, take it out of the $() you have it in in order to register it in the window namespace
function rlAl() {
  if ($("#xicon1").hasClass("active")) {
    $("#actionlist").load("alcurrent.php");
  }
  else if ($("#xicon2").hasClass("active")) {
    alert("icon2");
  }
  else if ($("#xicon3").hasClass("active")) {
    alert("icon3");
  }
}
$(function() {
  rlAl(); // so it executes straight away on DOM ready
  setInterval(rlAl, 5000);
});
    来源:https://stackoverflow.com/questions/5013940/jquery-load-something-based-on-whose-class-is-active