jQuery load something based on whose class is “active”

此生再无相见时 提交于 2020-01-14 05:17:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!