简体   繁体   English

有人可以帮我在 javascript 中写这个吗

[英]Can someone help me write this in javascript

I am trying to write this code in javascript from jquery我正在尝试从 jquery 在 javascript 中编写此代码

I already tried but cant seems to get equivalent to work.我已经尝试过,但似乎无法等同于工作。

$(document).ready(function() {

  $('a').click(function() {

    var selected = $(this);
    $('a').removeClass('active');
    $(selected).addClass('active');
  });

  var $a = $('.a'),
    $b = $('.b'),
    $c = $('.c'),
    $d = $('.d'),
    $home = $('.home'),
    $about = $('.about');

  $a.click(function() {

    $home.fadeIn();
    $about.fadeOut();
  });

  $b.click(function() {

    $home.fadeOut();
    $about.fadeIn();
  });
});

The code works perfect in jQuery, but am trying to just use javascript.该代码在 jQuery 中完美运行,但我试图只使用 javascript。 Its basically to add and remove class when a nav item is selected.它基本上是在选择导航项时添加和删除 class 。 I don't know if am explaining as clear as possible but am try to write the equivalent of this in javascript.我不知道我是否尽可能清楚地解释,但我尝试在 javascript 中写出与此等效的内容。

This is what I have tried.这是我尝试过的。

var callback = function(){ var回调=函数(){

 var clickHandler1 = function() { 

        document.getElementById("home").classList.remove("home"); 

       //var rem = document.getElementById("home");
       //fadeOut(rem);
       //alert("I am clicked B");
};

var anchors1 = document.getElementsByClassName("b");
for (var i = 0; i < anchors1.length; i++) {
        var current = anchors1[i];
        current.addEventListener('click', clickHandler1, false);
}

function fadeOut(el){
    el.style.opacity = 1;

    function fade() {
        if ((el.style.opacity -= .1) < 0) {
             el.style.display = "none";
        } else {
             requestAnimationFrame(fade);
          }
        })();
    };              

    fadeIn(el, display){
        el.style.opacity = 0;
        el.style.display = display || "block";

        (function fade() {
             var val = parseFloat(el.style.opacity);
             if (!((val += .1) > 1)) {
                 el.style.opacity = val;
                 requestAnimationFrame(fade);
             }
        })();
    };


 }

 if ( document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) {

  callback();

} else {

    document.addEventListener("DOMContentLoaded", callback);

}

The code in es6: es6中的代码:

window.onload = function() {
  const allLinks = document.querySelectorAll('a')

  console.log(allLinks)

  const removeAllClass = () => {
    allLinks.forEach(link => {
      link.classList.remove('active')
    })
  }


  allLinks.forEach(element => {
    element.addEventListener('click', event => {
      removeAllClass()
      element.classList.add('active')
    })
  })

  let $a = document.querySelectorAll('.a'),
  $b = document.querySelectorAll('.b'),
  $home = document.querySelector('.home'),
  $about = document.querySelector('.about');

  $a.forEach(element => {
    element.addEventListener('click', event => {
      $about.classList.remove('fadeIn');
      $home.classList.add('fadeIn');      
    })
  })

  $b.forEach(element => {
    element.addEventListener('click', event => {
      $home.classList.remove('fadeIn');
      $about.classList.add('fadeIn');
    })
  })

}

you can see working on https://codepen.io/rwladyka/pen/qBBBpvy你可以看到https://codepen.io/rwladyka/pen/qBBBpvy

document.getElementsByClassName('a') is the javascript equivalent of $('.a') document.getElementsByClassName('a') 是 $('.a') 的 javascript 等价物

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

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