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 に答える