Selecting text in an element (akin to highlighting with your mouse)

后端 未结 16 1663
孤街浪徒
孤街浪徒 2020-11-21 05:58

I would like to have users click a link, then it selects the HTML text in another element (not an input).

By \"select\" I mean the same way you would select

相关标签:
16条回答
  • 2020-11-21 06:27

    For any tag one can select all text inside that tag by this short and simple code. It will highlight the entire tag area with yellow colour and select text inside it on single click.

    document.onclick = function(event) {
        var range, selection;
    event.target.style.backgroundColor = 'yellow';
            selection = window.getSelection();
            range = document.createRange();
            range.selectNodeContents(event.target);
            selection.removeAllRanges();
            selection.addRange(range);
    };
    
    0 讨论(0)
  • 2020-11-21 06:27

    Added jQuery.browser.webkit to the "else if" for Chrome. Could not get this working in Chrome 23.

    Made this script below for selecting the content in a <pre> tag that has the class="code".

    jQuery( document ).ready(function() {
        jQuery('pre.code').attr('title', 'Click to select all');
        jQuery( '#divFoo' ).click( function() {
            var refNode = jQuery( this )[0];
            if ( jQuery.browser.msie ) {
                var range = document.body.createTextRange();
                range.moveToElementText( refNode );
                range.select();
            } else if ( jQuery.browser.mozilla || jQuery.browser.opera  || jQuery.browser.webkit ) {
                var selection = refNode.ownerDocument.defaultView.getSelection();
                console.log(selection);
                var range = refNode.ownerDocument.createRange();
                range.selectNodeContents( refNode );
                selection.removeAllRanges();
                selection.addRange( range );
            } else if ( jQuery.browser.safari ) {
                var selection = refNode.ownerDocument.defaultView.getSelection();
                selection.setBaseAndExtent( refNode, 0, refNode, 1 );
            }
        } );
    } );
    
    0 讨论(0)
  • 2020-11-21 06:29

    I was searching for the same thing, my solution was this:

    $('#el-id').focus().select();
    
    0 讨论(0)
  • 2020-11-21 06:31

    Jason's code can not be used for elements inside an iframe (as the scope differs from window and document). I fixed that problem and I modified it in order to be used as any other jQuery plugin (chainable):

    Example 1: Selection of all text inside < code > tags with single click and add class "selected":

    $(function() {
        $("code").click(function() {
            $(this).selText().addClass("selected");
        });
    });
    

    Example 2: On button click, select an element inside an Iframe:

    $(function() {
        $("button").click(function() {
            $("iframe").contents().find("#selectme").selText();
        });
    });
    

    Note: remember that the iframe source should reside in the same domain to prevent security errors.

    jQuery Plugin:

    jQuery.fn.selText = function() {
        var obj = this[0];
        if ($.browser.msie) {
            var range = obj.offsetParent.createTextRange();
            range.moveToElementText(obj);
            range.select();
        } else if ($.browser.mozilla || $.browser.opera) {
            var selection = obj.ownerDocument.defaultView.getSelection();
            var range = obj.ownerDocument.createRange();
            range.selectNodeContents(obj);
            selection.removeAllRanges();
            selection.addRange(range);
        } else if ($.browser.safari) {
            var selection = obj.ownerDocument.defaultView.getSelection();
            selection.setBaseAndExtent(obj, 0, obj, 1);
        }
        return this;
    }
    

    I tested it in IE8, Firefox, Opera, Safari, Chrome (current versions). I'm not sure if it works in older IE versions (sincerely I don't care).

    0 讨论(0)
提交回复
热议问题