简体   繁体   English

用coffeescript删除课程

[英]remove class with coffeescript

I want to remove the hidden class if the screen size reaches a certain size with coffeescript. 如果屏幕尺寸达到了带有CoffeeScript的特定尺寸,我想删除隐藏的类。 Here is my current code: 这是我当前的代码:

coffeescript: 咖啡脚本:

$(window).resize "form.edit_customization", (event) =>
  if $(window).width() <= 768
    removeClass '.hidden'

HTML: HTML:

<p class="hidden">Please use desktop or larger display when editing an event</p>

You do not need JS to do that, CSS would be enough, something like this should work: 您不需要JS来做,CSS就足够了,像这样的东西应该可以工作:

HTML: HTML:

<p class="small-screen-message">Please use desktop or larger display when editing an event</p>

CSS: CSS:

.small-screen-message {
  display: none;
}

@media (max-width: 768px) {
  .small-screen-message {
    display: inline;
   }
}

Here is an example JS bin 这是一个示例JS bin

You need to tell the removeClass function from which element the class should be removed. 您需要告诉removeClass函数应该从哪个元素中删除该类。 So if you want to remove the hidden-class from all elements that currently have that class you can write 因此,如果要从当前具有该类的所有元素中删除隐藏类,则可以编写

$(".hidden").removeClass "hidden"

However, you won't be able to select all those elements again if the screensize goes back to above 768px. 但是,如果屏幕尺寸回到768px以上,您将无法再次选择所有这些元素。 To be able to do that you can add a can-be-hidden class to those elements. 为此,您可以向这些元素添加一个can-be-hidden类。 So your HTML would be 所以你的HTML将是

<p class="hidden can-be-hidden">Lorem Ipsum</p>

and the JS 和JS

if $(window).width() <= 768
  $(".can-be-hidden").removeClass 'hidden'
else
  $(".can-be-hidden").addClass 'hidden'

PS removeClass '.hidden' will look for elements like <p class=".hidden"> , not <p class="hidden"> so better don't use a dot here PS removeClass '.hidden'将查找类似<p class=".hidden">元素,而不是<p class="hidden">因此最好不要在此处使用点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM