代码之家  ›  专栏  ›  技术社区  ›  dimButTries

DOM元素链接的基本方法

  •  3
  • dimButTries  · 技术社区  · 6 年前

    add() , subtract()

    this.textContent 行。我怎么得到 getId

    'use strict'
    let chain = {
        getId(id) {
            document.getElementById(id);
            return this;
        },
        text(content) {
            this.textContent = content;
            return this;
        }
    }
    window.addEventListener('load', function() {
        let dummy = chain.getId('dummy').text('works');
    })
    <div id='dummy'></div>
    1 回复  |  直到 6 年前
        1
  •  1
  •   Gerardo Furtado    6 年前

    a comment this element 属性,将其设置为 getId text . 例如:

    'use strict'
    let chain = {
      element: null,
      getId(id) {
        this.element = document.getElementById(id);
        return this
      },
      text(content) {
        this.element.textContent = content;
        return this;
      }
    }
    window.addEventListener('load', function() {
      let dummy = chain.getId('dummy').text('works');
    })
    <div id='dummy'></div>