📜  wtich js - Javascript (1)

📅  最后修改于: 2023-12-03 15:06:02.952000             🧑  作者: Mango

Witch JS - Javascript

Witch JS is a lightweight, open-source library for handling DOM events and manipulations in modern browsers using Javascript. It aims to simplify and streamline event handling by providing a straightforward API and reducing the need for boilerplate code.

Key Features
  • Easy-to-use API for handling events.
  • Supports all modern browsers.
  • Lightweight and efficient.
  • No dependencies.
Installation

You can install Witch JS by including the following script tag in your HTML file:

<script src="https://cdn.jsdelivr.net/npm/witch-js/dist/witch.min.js"></script>

Alternatively, you can install it via npm:

npm install witch-js --save
Usage

To use Witch JS, simply call the witch() function with the element selector as the argument. Then, chain the event you want to handle using the on() method, passing in the event type and the handler function.

witch('#myBtn').on('click', function(event) {
  alert('Button clicked!');
});

You can also use the off() method to remove event handlers:

var myBtn = witch('#myBtn');

// Add event handler
myBtn.on('click', myClickHandler);

// Remove event handler
myBtn.off('click', myClickHandler);
API Reference
witch(selector: string): Witch

Creates a new instance of Witch for the specified element(s). Returns a Witch object.

Parameters

  • selector: string - The element selector.

Returns

  • Witch - A Witch instance for the specified element(s).
on(type: string, handler: function): Witch

Registers an event listener for the specified event type. Returns the Witch instance.

Parameters

  • type: string - The event type.
  • handler: function - The event handler function.

Returns

  • Witch - The Witch instance.
off(type: string, handler: function): Witch

Removes an event listener for the specified event type. Returns the Witch instance.

Parameters

  • type: string - The event type.
  • handler: function - The event handler function.

Returns

  • Witch - The Witch instance.
Conclusion

If you're looking for a lightweight and easy-to-use library for handling DOM events and manipulations in modern browsers, Witch JS is a great choice. Its simple API and lack of dependencies make it perfect for small projects or quick prototypes, while its browser compatibility ensures it will work well in a production environment.