1

我想知道我的 jquery 函数是否正确

<script>
window.blinker = setInterval(function(){
  if(window.alerta){
    $('a.cadastrotopo').css('color','#346698');
    $('a.cadastrotopo').css('text-decoration','underline');
      window.alerta=false;
    }
    else{
      $('a.cadastrotopo').css('color','#000');
      $('a.cadastrotopo').css('text-decoration','none');
      window.alerta = true;
    }
},500);
</script>

工作正常,但我想知道我是否做对了。

我谢谢你。

4

2 回答 2

9

就我个人而言,我会更多地使用 CSS,尤其是类:

a.cadostropo {
  color: #000;
  text-decoration: none;
}
a.alert { 
  color: #346698;
  text-decoration: underline;
}

然后解决方案变得微不足道:

setInterval(toggleAlert, 500);

function toggleAlert() {
  $("a.cadostropo").toggleClass("alert");
}

附注:css()您可以使用匿名对象来指定多个属性,而不是多次调用。

$("a.cadostropo").css({color: "#346698", textDecoration: "underline"});

话虽如此,我不喜欢像这样使用硬编码的 CSS 操作。偏爱班级。它们更加灵活,您不必担心破坏性更改和回滚它们。

于 2010-06-25T09:56:54.213 回答
0

是的,您是,尽管您可以将 css 声明组合成这样。

.css({'color' : "#346698", 'text-decoration' : "underline"});
于 2010-06-25T09:57:18.403 回答