Home  >  Article  >  Web Front-end  >  List of common jQuery events

List of common jQuery events

WBOY
WBOYOriginal
2024-02-24 11:57:06423browse

List of common jQuery events

[List of commonly used events in jQuery, specific code examples are required]

jQuery is a popular JavaScript library that is widely used in web development. In jQuery, event handling is a very important part. Through events, we can achieve interactive and dynamic effects on the page. This article will introduce commonly used events in jQuery, including click events, mouse events, keyboard events, etc., and provide specific code examples.

1. Click event

1. click event

The click event is an event triggered when an element is clicked. It can be obtained through the click() method. Bind the handler function of the click event.

$("#btn").click(function(){
    alert("按钮被点击了!");
});

2. dblclick event

The dblclick event is an event triggered when an element is double-clicked. The handler function of the dblclick event can be bound through the dblclick() method.

$("#box").dblclick(function(){
    alert("盒子被双击了!");
});

2. Mouse events

1. mouseenter and mouseleave events

mouseenter and mouseleave events are triggered when the mouse enters and leaves the element respectively. They can be passed mouseenter( ) and mouseleave() methods to bind the handler function.

$("#box").mouseenter(function(){
    $(this).css("background-color", "red");
}).mouseleave(function(){
    $(this).css("background-color", "white");
});

2. Hover event

The hover event is triggered when the mouse enters and leaves the element, and the handler function can be bound through the hover() method.

$("#box").hover(function(){
    $(this).css("background-color", "blue");
}, function(){
    $(this).css("background-color", "white");
});

3. Keyboard events

1. keydown, keypress and keyup events

keydown, keypress and keyup events are triggered when the keyboard keys are pressed, held down and released respectively , the processing function can be bound through the corresponding method.

$(document).keydown(function(event){
    console.log("按下了键:" + event.key);
});

$(document).keypress(function(){
    console.log("按住键不放。");
});

$(document).keyup(function(){
    console.log("释放了键。");
});

4. Other common events

In addition to the above-mentioned common events, jQuery also supports other events, such as change, submit, resize, etc. These events can also be bound to handler functions through corresponding methods.

$("#input").change(function(){
    alert("输入框内容发生了变化。");
});

$("#form").submit(function(){
    alert("表单提交了。");
});

$(window).resize(function(){
    alert("窗口大小发生了变化。");
});

In projects, reasonable use of these events can add interactivity and user experience to the page. I hope the content of this article can be helpful to the majority of developers.

Conclusion

Through the introduction of this article, we have learned about the commonly used events in jQuery and their corresponding code examples. These events are commonly used interactive methods in web development, and mastering them can help developers achieve richer page effects. I hope readers can improve their front-end skills through practice and continuous learning.

The above is the detailed content of List of common jQuery events. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn