📅  最后修改于: 2023-12-03 15:06:02.952000             🧑  作者: Mango
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.
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
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);
witch(selector: string): Witch
Creates a new instance of Witch for the specified element(s). Returns a Witch
object.
selector: string
- The element selector.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.
type: string
- The event type.handler: function
- The event handler function.Witch
- The Witch
instance.off(type: string, handler: function): Witch
Removes an event listener for the specified event type. Returns the Witch
instance.
type: string
- The event type.handler: function
- The event handler function.Witch
- The Witch
instance.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.