Giter Club home page Giter Club logo

Comments (1)

azl397985856 avatar azl397985856 commented on June 5, 2024

这是一个考察递归的题目, 但是需要考虑的内容又、有很多。 比如selector支持什么? 你可能知道又id,class,元素,以及一些级联选择器,如果想要完整实现还是需要一定的代码量和心思缜密程度的。
这一点一定要在开始做之前想到。

为了简单起见,我这里只实现了class选择器,剩下的交给读者来完善。 JS代码:

function matchSelector(selector, element) {
  if (Array.prototype.includes.call(element.classList, selector.slice(1))) return true;
  return false;
}
function querySelector(selector, element) {
  if (element === null) return null;
  const children = element.children;
  let res = null;
  for(let child of children) {
    if (matchSelector(selector, child)) return child;
    res = querySelector(selector, child);
    if (res !== null) return res;
  }
  return null;
}

querySelector('.test', document.body);

from fe-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.