this.offset is not a function within a click function

前端 未结 2 1475
攒了一身酷
攒了一身酷 2021-01-12 02:33

The error is that _this.offset is not a function. I logged this to the console and it was the

  • element I clicked on, so I am confused why
  • 2条回答
    •  暗喜
      暗喜 (楼主)
      2021-01-12 03:06

      Replace these two lines:

      var topx = _this.offset().top;
      var leftx = _this.offset().left;
      

      with:

      var topx = _this.offsetTop;
      var leftx = _this.offsetLeft;
      

      As .offset() is a jquery function and _this is a DOM element.


      Also for your .css line you have to wrap _this in $(...) again because _this is a DOM element and **not a jQuery object.

    提交回复
    热议问题