-3

我正在使用一个简单的脚本,它从我的字段中删除 css 类。

<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
  $('#myField').removeAttr('class');
}, 200);
 });
</script>

该脚本仅在我使用 setInterval() 时才有效,否则该脚本将不起作用。

有人可以帮我如何在没有 setInterval() 的情况下执行脚本,我猜这是因为脚本在页面完全加载之前执行。

4

1 回答 1

0

显然,id 为 myField 的元素是由脚本动态创建的。问题是元素可能不会创建到 200 毫秒。最好做一些其他的测试。不幸的是,没有其他方法,除非您更改创建元素的周围代码。更优选地,代码以这种方式编写。至少通过删除元素来停止间隔。

<script type="text/javascript">
/**
 *  @author Georgi Naumov 
 **/
$(document).ready(function() {
    setTimeout(function handleItem() {
      if($('#myField').length) {
          $('#myField').removeAttr('class');
      } else {
          setTimeout(handleItem, 200);
      }
    }, 200);
 });
</script>
于 2013-10-29T06:42:22.777 回答