Select all text on hover?
I saw this the other day online and it intrigued me. The site had several strings of text for embedding video, pictures, etc. Wh开发者_StackOverflowat was neat was when I hovered over them, all of the text in the text box was selected, making it easier to copy/paste. I'm curious as to how this was done.
You don't even need jQuery for this.
<input onmouseover="this.select()" />
HTML
<textarea class="auto_select"></textarea>
jQuery
$(".auto_select").mouseover(function(){
$(this).select();
});
Just add the jQuery in your global jQuery library and then add the class on each element that you want to select on hover.
HTML:
<input type="text" id="test" value="Just some text here">
JavaScript:
$('#test').mouseenter(function() {
this.focus();
this.select();
});
Live demo: http://jsfiddle.net/5F8Wm/
Edit: Oops! Didn't see you wanted jQuery! This is it with no library:
var el = document.getElementById("your-textarea");
if (el.addEventListener) el.addEventListener("mouseover",selectText,false);
else if (el.attachEvent) el.attachEvent("onmouseover",selectText);
else el.onmouseover = selectText;
function selectText(){
this.focus();
this.select();
}
See a jsfiddle here: http://jsfiddle.net/GBgJ9/
<input type="text" onmouseover="this.select();" id="textAreaId" name="textArea"/>
You can use this onmouseover
or onclick
or anywhere you want. Is that what you wanted?
$("textarea").hover(function(){
$(this).select();
});
You can use something like this:
$("input").mouseover(function() {
$(this).select();
});
精彩评论