如何计算DOM元素内的文本行?我可以吗? [英] How can I count text lines inside an DOM element? Can I?

查看:86
本文介绍了如何计算DOM元素内的文本行?我可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方式来计算div内的行数。说我们有一个这样的div:

I'm wondering if there's a way to count lines inside a div for example. Say we have a div like so:

<div id="content">hello how are you?</div>

根据很多因素,div可以有一个或两个,甚至四行文本。有没有办法知道脚本?

Depending on many factors, the div can have one, or two, or even four lines of text. Is there any way for the script to know?

换句话说,DOM中是否表示自动中断?

In other words, are automatic breaks represented in DOM at all?

推荐答案

我相信现在是不可能的。这是,但是。

I am convinced that it is impossible now. It was, though.

IE7的getClientRects的实现完全是我想要的。在IE8中打开此页,尝试刷新它窗口宽度,并查看第一个元素中的行数如何相应地更改。以下是该页面的JavaScript的关键行:

IE7’s implementation of getClientRects did exactly what I want. Open this page in IE8, try refreshing it varying window width, and see how number of lines in the first element changes accordingly. Here’s the key lines of the javascript from that page:

var rects = elementList[i].getClientRects();
var p = document.createElement('p');
p.appendChild(document.createTextNode('\'' + elementList[i].tagName + '\' element has ' + rects.length + ' line(s).'));

不幸的是,Firefox总是为每个元素返回一个客户矩形,而IE8现在也是一样。 (Martin Honnen的页面今天工作,因为IE在IE兼容视图中呈现;在IE8中按F12可以播放不同的模式。)

Unfortunately for me, Firefox always returns one client rectangle per element, and IE8 does the same now. (Martin Honnen’s page works today because IE renders it in IE compat view; press F12 in IE8 to play with different modes.)

这很可悲。它看起来像是Firefox的文字,但毫无价值地实现规范赢得了微软的有用的。或者我想念新的getClientRect可能会帮助开发人员的情况?

This is sad. It looks like once again Firefox’s literal but worthless implementation of the spec won over Microsoft’s useful one. Or do I miss a situation where new getClientRects may help a developer?

这篇关于如何计算DOM元素内的文本行?我可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆