简体   繁体   English

Internet Explorer getClientRects绝对定位的元素

[英]Internet Explorer getClientRects absolute positioned elements

I'm currently experiencing an issue with text selection in IE 11 (though it happens in previous IEs as well). 我目前在IE 11中遇到文本选择问题(尽管在以前的IE中也会发生)。 My goal is to get text selection working where it returns to me the getClientRects. 我的目标是使文本选择在返回给我getClientRects的地方起作用。 What I have is a simple setup. 我有一个简单的设置。 I have a container div and within that div I've placed two spans: 我有一个容器div,在该div中,我放置了两个跨度:

<div onmouseup="mouseup()" id="container">
    <div class="span1">Span 1</div>
    <div class="span2">Span 2</div>
</div>

Both spans are positioned absolutely to the top/left and bottom/right corners. 两个跨度均绝对位于上/左和下/右角。 The container has a height of 100px; 容器的高度为100像素;

On mouse up this function is ran: 将鼠标移到该函数上即可运行:

function mouseup(){
    var selectObj = ((window.getSelection)?window.getSelection():document.getSelection());
    alert(selectObj.getRangeAt(0).getClientRects().length);
}

On chrome if you try to select the text from span 1 to span 2 it will alert a length of 3, which is what I want as that lets me know where the bounding boxes are for the text selection. 在chrome上,如果您尝试从跨度1到跨度2选择文本,则会提示长度为3,这正是我想要的,因为这让我知道了文本选择的边界框在哪里。 On internet explorer though it always returns 0. If I select text only within a single span it will work, but as soon as you go to multiple it fails. 在Internet Explorer上,尽管它始终返回0。如果我仅在单个跨度内选择文本,它将起作用,但是一旦您转到多个,它将​​失败。

Here is a jsFiddle of this all set up: http://jsfiddle.net/v6sa71hs/ 这是所有设置的jsFiddle: http : //jsfiddle.net/v6sa71hs/

Looks like you just can't do it that way. 看来您无法那样做。 If you instead tell everything to float and then position it with margin-top/left it will work. 如果您改为告诉所有内容浮动,然后将其定位在margin-top / left处,则它将起作用。 Here is the css for both spans 这是两个跨度的css

span{
    float: left;
    margin-bottom: -100%;
    margin-right: -100%;
}

Updated fiddle: http://jsfiddle.net/9gemwtc5/ 更新小提琴: http : //jsfiddle.net/9gemwtc5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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