获得选中文本的像素

[code="html"]



<br> .demo{<br> text-overflow:clip;<br> width:100px;<br> height:100px;<br> overflow:hidden;<br> border:1px solid red;<br> white-space:nowrap;<br> }<br>


哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈



[/code]

div设置了text-overflow属性,它的内容像素大于div的宽度,选中div中的文字,怎么获得文字的像素?

不同的browser的默认字体大小(px)可能不一样。
另外,选中的字体,可能有多种不同的"style".
所以,基本是没有办法确认的。

但是,可以确认选中的文本。

function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}

 return txt;

}

获得Graphics 应用Graphics.MesureString方法,应用它当前的字段,获取长度

打开qq,ctrl+alt+a,量一下