jQuery load something based on whose class is “act

2019-06-06 15:59发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-06-06 16:51

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);
});
查看更多
登录 后发表回答