我的jquery正确吗?

发布于 2024-09-06 16:16:12 字数 485 浏览 2 评论 0原文

我想知道我的 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>

工作正常,但我想知道我的方法是否正确。

我谢谢你。

I am wondering if my jquery function is correct

<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>

is working ok, but I wonder if I'm doing the right way.

I thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2024-09-13 16:16:12

就我个人而言,我会更多地使用 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 操作。青睐班级。它们更加灵活,您不必担心破坏性的更改并将其回滚。

Personally I would use CSS more, particularly classes:

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

and then the solution becomes trivial:

setInterval(toggleAlert, 500);

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

One side note: instead of multiple css() calls you can use anonymous objects to specify multiple properties.

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

That being said, I prefer not to use hardcoded CSS manipulation like this. Favour classes. They're far more flexible and you don't have to worry about destructive changes and rolling them back.

时光无声 2024-09-13 16:16:12

是的,确实如此,尽管您可以将 css 声明组合起来,如下所示。

.css({'color' : "#346698", 'text-decoration' : "underline"});

Yes, you are, although you could combine the css declarations to look like this.

.css({'color' : "#346698", 'text-decoration' : "underline"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文