最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Detecting an "unselect" event in a text area - Stack Overflow

matteradmin7PV0评论

jQuery has the .select() method, which lets you execute a function when text is selected in a text area.

Unfortunately, there is no corresponding .deselect() method for executing a function when the user removes a selection.

How can I detect when the user has unselected the text (for example, by typing, clicking, or defocusing)?

jQuery has the .select() method, which lets you execute a function when text is selected in a text area.

Unfortunately, there is no corresponding .deselect() method for executing a function when the user removes a selection.

How can I detect when the user has unselected the text (for example, by typing, clicking, or defocusing)?

Share Improve this question asked Mar 13, 2012 at 23:28 Amanda SAmanda S 3,2944 gold badges35 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4
$("textarea").select(function() {
    //do something here
});

$("textarea").on("blur focus keydown mousedown", function() {
    //do something else here
});​

.on is used in jquery 1.7 and later. Prior to that you can use .delegate or .live

http://jsfiddle/s29Vb/4/

It was simplified for the example. You can be more specific with the selector rather than $("textarea").

Another example if you want to do something on blur focus keydown mousedown only if text has been selected in the first place.

http://jsfiddle/s29Vb/5/

Post a comment

comment list (0)

  1. No comments so far