获取元素内文本的位置 [英] Get the position of text within an element
问题描述
如果我有一个元素,例如:
< p> Lorem ipsum dolor sit amet,ante risus massa adipiscing quisque quis。在mauris tellus中,sed vehicula整数fermentum rhoncus在faucibus,在简历purus。 Maecenas in vel ligula orci tellus ac,fringilla conubia lorem elit。 Dui nulla sodales morbi vel。马萨塞维沃拉。 Maecenas imperdiet donec urna a,ligula sed< / p>
这个过程贯穿多行:
Lorem ipsum dolor sit amet,ante risus massa adipiscing
quisque quis。在mauris tellus中,在faucibus,简单purus中,整数
fermentum rhoncus。 Maecenas
in lig ligula orci tellus ac,fringilla conubia lorem
elit。 Dui nulla sodales morbi vel。马萨塞维沃拉。
Maecenas imperdiet donec urna a,ligula sed
有可能找出位置(在x,y coords中)使用JavaScript的文字中的特定字符?如果没有这样做,我可以得到流动文本中每行的y位置?
请注意:< p>
标记,因此在每个字符/单词周围添加< span>
会导致处理过多。如果你知道文本中字符的字符串位置,你可以将字符包装在< span>中。
If I have an element, for instance:
<p>Lorem ipsum dolor sit amet, ante risus massa adipiscing quisque quis. At mauris tellus in, sed vehicula integer fermentum rhoncus at faucibus, in vitae purus. Maecenas in vel ligula orci tellus ac, fringilla conubia lorem elit. Dui nulla sodales morbi vel. Massa sed viverra. Maecenas imperdiet donec urna a, ligula sed</p>
And this flowed across multiple lines:
Lorem ipsum dolor sit amet, ante risus massa adipiscing
quisque quis. At mauris tellus in, sed vehicula integer
fermentum rhoncus at faucibus, in vitae purus. Maecenas
in vel ligula orci tellus ac, fringilla conubia lorem
elit. Dui nulla sodales morbi vel. Massa sed viverra.
Maecenas imperdiet donec urna a, ligula sed
Is it possible to find out the position (in x,y coords) of a particular character in the text using JavaScript? Failing this could I get the y position of each line in the flowed text?
Please note: There is a lot of text in the <p>
tag in my application, so adding <span>
around each character/word would be too much processing.
If you know the string position of the character in the text, you can wrap the character in a <span> or similar element with an ID and find the x,y coords of that element. Easiest way to do this is with jQuery or another library, see this answer for a cross-browser, no-library method.
这篇关于获取元素内文本的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!