Here is the method I came up with.
It uses <span>
instead of the <a>
tag, but behaves like a regular link with some bonuses. When you view the text, there is a timeout of 500 milliseconds, which will change the cursor to the text selection cursor. Clicking and dragging the text does not cause a click action, however double-clicking on the text (to select whole words) still causes the click event. It can be improved, but it works well and can be used in production.
Another opportunity to do it even better when the timeout is executed, it also shows a small clipboard icon in the upper right corner of the button that they can click to copy the text to the clipboard. When you .mouseout()
button, it will hide this clipboard icon.
In addition, another function may be to clear the selection of text when the mouse leaves the button. ( Clear text selection using JavaScript )
HTML
<span data-href="javascript:alert('This is the click action.');" class="link selectable"> <span> info@mydomain.com </span> </span>
CSS
.link { font: 16px/0 Tahoma, sans-serif; padding: 15px 30px; border: 1px solid #0079be; border-radius: 4px; color: #0079be; cursor: pointer; } .link:hover { color: #fff; background: #0079be; } .link.selectable > span { display: inline-block; } .link.selectable.selecting > span { cursor: text; }
Javascript
$('.link.selectable > span').hover( function() { var selectableTimeout = setTimeout( function(elem) { $(elem).parent().addClass('selecting'); }, 500, this ); $(this).data('selectableTimeout', selectableTimeout); }, function() { clearTimeout($(this).data('selectableTimeout')); $(this).parent().removeClass('selecting'); } ); $('.link.selectable').mousedown( function(e) { $(this).data('mouseX', e.pageX); $(this).data('mouseY', e.pageY); } ); $('.link.selectable').mouseup( function(e) { var mouseX = $(this).data('mouseX'); var mouseY = $(this).data('mouseY'); if (mouseX && mouseY && mouseX === e.pageX && mouseY === e.pageY) { window.location.href = $(this).attr('data-href'); } } );
https://jsfiddle.net/gavin310/6efdyq4h/
Gavin source share