How to make a function to go after an element

This question already has an answer here:

  • How does JavaScript .prototype work? 21 answers

  • 你可以使用原型来实现它

    // `getElementById` returns a `Element` object so extend it's prototype
    Element.prototype.someFunction = function(){
       // this.somethig
    }
    
    document.getElementById("element").someFunction();
    

    Element.prototype.someFunction = function() {
      console.log(this.value)
    }
    
    document.getElementById("element").someFunction();
    <input value="abc" id="element" />

    You are looking for prototypes

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

    element.prototype.someFunction = function(){
      this.doSomething(); // this refers to element
    }
    
    链接地址: http://www.djcxy.com/p/30072.html

    上一篇: 原型属性抛出undefined

    下一篇: 如何制作一个功能去追逐元素