0

Here is HTML code. It supposed to select all text in "Container" div

<B onclick="SelectText(document.getElementById('Container'));">select all text</B>
<Div id="Container">
<Div>123456</Div>
<Div>123456</Div>
<Div onclick="SelectText();">123456</Div>
</Div>

here is JS code of the SelectText() function

function SelectText(target){
if(target==null){
var e = window.event || e;
if (!e) var e = window.event;
var target=e.target || e.srcElement;
}

var rng, sel;
if ( document.createRange ) {
rng = document.createRange();
rng.selectNode( target );
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange( rng );
} else {
var rng = document.body.createTextRange();
rng.moveToElementText( target );
rng.select();
    }
}

Problem is that in Opera 12.02 when "select all text" is clicked, all text seems like selected, but it's not selected (I can't rightclick it and copy).

(terrific, but IE works fine with it)

Why not in Opera?!!! And what can I do to make Opera 12.02 believe that all text in "Container" is selected?

4

1 回答 1

1

That code works fine for me in 12.11 on jsfiddle, so I guess you just need to use a more recent Opera version. I remember older Opera versions having bugs like that.

于 2012-12-03T11:01:30.913 回答