Why is the event handle binding function written by myself useless?

the front-end beginner wrote a simple event handle binding function.

<!DOCTYPE html>
<html>
<head>
    <title>class operation demo</title>
    <style type="text/css">
        .b1{
            width: 100px;
            height: 100px;
            background-color: yellow;
        }

        .b2{
            width: 300px;
            height: 300px;
            background-color: red;
        }
    </style>

    <script type="text/javascript">
        
        window.onload = function() {
            var btn01 = document.getElementById("btn01");
            var box = document.getElementById("box");

            function bind(obj,eventStr,callback) {
            if ( obj.addEventListener )
                obj.addEventListener(eventStr,callback,false);
            else
                obj.attachEvent("on" + eventStr,function(){
                    callback.call(obj);
                });
            }

            function changeClass() {
                // this.className += " b2";
                alert("test");
            }
            
            bind(btn01,"click",changeClass);
        };
    </script>
</head>
<body>
    <button id="btn01">box</button>
        
    <br /><br />
        
    <div id="box" class="b1"></div>
</body>
</html>
The

question has been modified, and the question now is: I want to use this button to change the class that specifies div (for example, the id in body is the div of box), but how do I modify the bind function? Or is there a better solution?

Mar.17,2021

bind (btn01, "click", changeClass); pass in a function
you pass the return value of the function
add:
write you a pseudo-jq (jq class that is not written in this way)

function $(select) {
//id
    var el = select instanceof HTMLElement?select:document.getElementById(select);
    var r = {
        bind: function (eventStr, callback) {
            if (el.addEventListener)
                el.addEventListener(eventStr, callback, false);
            else
                el.attachEvent("on" + eventStr, callback);
            return this
        },
        addClass:function (str){
          if(!this.hasClass(str)){
            el.className = el.className+str
          }
          return this
        },
      hasClass:function(str){
        return el.className.split(' ').indexOf(str)>-1
      },
    };

    return r
}

call:

//id  dom
$("btn").bind('click', function (e) {
  $(this).addClass('active')
})

window.onload = function () {
    var btn01 = document.getElementById("btn01");
    var box = document.getElementById("box");

    function bind(obj, eventStr, callback) {
        if (obj.addEventListener) {
            obj.addEventListener(eventStr, callback, false);
        } else {
            obj.attachEvent("on" + eventStr, function () {
                callback.call(obj);
            });
        }
    }

    function changeClass() {
        this.className += " b2";
        alert("test");
    }

    bind(btn01, "click", changeClass);
};

bind (btn01, "click", changeClass); changeClass returns the callback function instead of the finished thing
code indentation is done, even if there is only one sentence, add {}


bind (the function changeClass () has been executed after the btn01, "click", changeClass ()) page is loaded, change it to bind (when btn01, "click", changeClass); clicks the button again, alert ("test")

function changeClass () {

    box.className += ' b2'
    // alert("test");
  }
  
  
  
  b2

I have read node's crawl article before and asked why some right-click to open the source code. There is almost nothing left

Menu