我的jquery这个正确吗?

Pat*_*que 1 css jquery hyperlink blink

我想知道我的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>
Run Code Online (Sandbox Code Playgroud)

工作正常,但我想知道我是否采取了正确的方式.

我谢谢你.

cle*_*tus 9

我个人会更多地使用CSS,特别是类:

a.cadostropo {
  color: #000;
  text-decoration: none;
}
a.alert { 
  color: #346698;
  text-decoration: underline;
}
Run Code Online (Sandbox Code Playgroud)

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

setInterval(toggleAlert, 500);

function toggleAlert() {
  $("a.cadostropo").toggleClass("alert");
}
Run Code Online (Sandbox Code Playgroud)

一方面注意:css()您可以使用匿名对象指定多个属性,而不是多个调用.

$("a.cadostropo").css({color: "#346698", textDecoration: "underline"});
Run Code Online (Sandbox Code Playgroud)

话虽这么说,我宁愿不使用像这样的硬编码CSS操作.赞成课程.它们更加灵活,你不必担心破坏性的变化并将它们推回去.