JavaScript DOM object to jQuery object

后端 未结 2 1179
半阙折子戏
半阙折子戏 2021-02-01 01:19

How can I convert a JavaScript DOM object to a jQuery object?



function changeStatus(myObject) {
       XXX.removeClass         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 01:42

    var $this = $(myObject);
    

    $this is a jQuery object. You can create jQuery objects from DOM elements.

    
    
    function changeStatus(myObject) {
           $(myObject).removeClass();
    }
    

    I would like to recommend doing your event binding with jQuery as well:

    
    
    $('.change-status').on('click', function () {
        $(this).removeClass( ... );
    });
    

    This is nice because now all the JS code is in one place and can be updated (in my opinion) more easily. Note that the class I added to the element is not necessary if you want to bind to all elements in the DOM.

提交回复
热议问题