简体   繁体   English

设置默认类 Javascript

[英]set default class Javascript

I am very much new to Javascript and I could need a little help with the following problem:我对 Javascript 非常陌生,我可能需要一些帮助来解决以下问题:

I want to use the following function to make a header that closes and opens upon clicking it and I stumbled upon this function, in which I added the 'closed' class, which I would like to set as a default when the page is loaded initially.我想使用以下函数来制作一个在单击时关闭和打开的标题,我偶然发现了这个函数,我在其中添加了“关闭”类,我想在最初加载页面时将其设置为默认值. Does somebody have an idea for me, I would be very much grateful!有人对我有想法吗,我将不胜感激!

$(function() {
  var b = $("#contact");
  var w = $("#button");
  var l = $("#list");

  w.height(l.outerHeight(true));

  b.click(function() {

  if(w.hasClass('closed')) {
  w.removeClass('closed');
  w.addClass('open');
  w.height(l.outerHeight(true));
} else {
  w.addClass('closed');
  w.height(0);
}

  });
});

The function you pass to $(…) is called once when the page initially loads.您传递给$(…)的函数在页面最初加载时被调用一次。 You already use that to set up the click handler that toggles the closed class.您已经使用它来设置切换closed类的点击处理程序。 So in addition to setting the click handler, you can also just add the class there:所以除了设置点击处理程序之外,您还可以在那里添加类:

$(function() {
  …
  b.click(function() {
    …
  });

  // set the initial class value
  w.addClass('closed');
});

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

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