简体   繁体   中英

can't select text in textarea in IE

I've got a couple of cases where I use a textarea to display some source code for database queries. The idea is to allow the user to select all the text in the textarea so they can copy that code and paste it some where else. All is good and well except in IE-{8,9} which are the only two IE browsers I care about.

The selection works fine for one textarea and not for the other. I've racked my brain over this for hours with no luck. I've tried to reproduce this in a tiny code snippet to illustrate the problem but that hasn't worked either.

So to go at it another direction is there a way to disable selecting text from a textarea that is IE specific and doesn't require specific javascript code? I'm not writing any specific code to disable the selection of text in a textarea.

for example if I have this textarea

<textarea>Some Text</textarea>

How might that text be unselectable in IE only? I've also tried selecting the text programatically using jquery, javascript, and DOM api. Which works in all browsers except IE-{8,9}.

Just for example.

$('textarea').select();

I've pondered the idea that some event listeners are not present, but since the IE developer tools sooo amazing I haven't had any luck tracking those down comparing what's listed in Chrome.

I've also reset IE and ensured copy 'drag and drop or copy and paste' is enabled.

I would chalk it up as a browser bug and write my own but since it work in one case and not the other it seems like something happening in the background I'm not seeing.

Select by ID works perfectly fine for me:

jsfiddle link

Yo can't refer to the textarea like this. You must use an id.

<textarea id="foo">Some Text</textarea>

<script type="text/javascript">
    $('#foo').select();
</script>

Well as it turns out there was a mixin that was overriding the onselectstart method.

    if (typeof self.node.onselectstart !== 'undefined') {
        self.node.onselectstart = function() {
            return false;
        };
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM