用 jQuery 检测换行符?
是否可以让 jQuery/javascript 检测字符串被破坏的位置(为了适应 CSS 宽度限制),以便在新行开始之前插入 DOM 元素?
is it possible to have jQuery/javascript detect where a string is broken (in order to fit into CSS width constraints) so as to insert DOM elements before the beginning of a new line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想出了一种方法,但对于您的目的来说可能有点过分了,所以请考虑到这一点。
您需要创建元素的克隆,清空原始元素,然后将每个单词移回到原始元素中。如果高度在任何时候发生变化,则该单词之前会有一个换行符。使用
$(el).text()
做到这一点相当简单,但如果里面可以有其他标签,而不仅仅是文本,那就会变得更加复杂。我尝试在这个答案框中解释如何按节点分解它,但发现在 jsFiddle 中创建 jQuery 插件更容易。链接在这里:http://jsfiddle.net/nathan/qkmse/ (它不能很好地处理浮动元素,并且在其他一些情况下它会掉落。如果您想要更多选项,或者它不太适合您的目的,或者您不确定如何应用它,请告诉我,我会尽力提供帮助。
I came up with an approach, but it might be overkill for your purposes, so take this into account.
You need to create a clone of the element, empty the original, then move each word back into the original element. If the height changes at any point, there's a line-break before that word. This would be fairly simple to do using
$(el).text()
, but it gets more complicated if there can be other tags inside, not just text. I tried explaining how to break it down by node in this answer box, but found it easier just to create a jQuery plugin in a jsFiddle. Link here: http://jsfiddle.net/nathan/qkmse/ (Gist).It won't handle floated elements all that well, and there are a few other situations where it'll fall over. Let me know if you'd like more options, or if it doesn't quite work for your purposes, or if you're not sure how to apply it and I'll try to help.
这是一种方法。注意:如果不使用等宽字体,我看不到理想的解决方案。与字符相等使这项任务变得更加容易。
查看 jsfiddle 相关的 html。我还没有完成这个功能。计算断裂指数时需要进行更多检查。现在它使用lastIndexOf(' '),但这忽略了下一个索引可能是空格或当前索引。另外,我没有考虑其他换行字符。然而,这应该是一个很好的起点。
http://jsfiddle.net/Y5Ftn/1/
Here is one approach. Note: I do not see a ideal solution without using monospace fonts. The equal with characters make this task much easier.
Have a look at the jsfiddle for associated html. I have not completed this function. More checks need to be put in when calculating the breaking index. Right now it is using lastIndexOf(' '), but this ignores that the next index could be a space, or the current. Also I am not accounting for other line-breaking characters. However this should be a great starting point.
http://jsfiddle.net/Y5Ftn/1/
这是我的脚本,它接受文本,然后使每一行成为一个跨度
CSS:
html
JS
你必须包含 jQuery。
this is my script, that takes text, and then makes each line a span
CSS:
html
JS
You have to got jQuery included.
或者,您可以将文本块的宽度与其父级的宽度进行比较。如果块的宽度至少是其父块的 98%,则非常确定它会损坏
Alternatively you could compare width of the text block with its parent's width. If the block is at least 98% of the width of its parent, pretty sure it breaks
我不知道任何内置的 jQuery 或 javascript 函数可以做到这一点。不过,您可以自己执行此操作,但如果您有大量文本,则可能会很慢。
理论上,您可以确保高度设置为自动,删除文本,然后逐字重新插入它。当高度发生变化时,您删除最后一个单词,然后插入 dom 元素。同样,如果有大量文本,这会很慢,更好的方法是不更改原始元素,而是在另一个可以隐藏的元素中进行更改,然后在完成后替换原始元素。这样,您的用户就不会看到内容消失然后逐字返回。
另一种方法是使用类似的原理,从一个相同大小且高度为 auto 的空元素开始,然后插入一个新字符和一个空格,直到得到一个新行。从那里,您可以将其用作上述技术的近似值,或者您可以盲目地添加每个字符串的长度,直到找到新行,同时考虑到 dom 元素的宽度。不过,这种技术对于等宽字体效果更好,这就是仅将其用作近似值的地方。
也就是说,有一种方法可以使用画布来测量文本,但这可能是极端的。理论上来说,创建一个画布元素,获取上下文,设置所有字体属性,然后使用
context.measureText()
方法。您可以在此处找到其使用示例。I don't know of any built in jQuery of javascript function to do this. You could, however, do it yourself, but it would be potentially slow if you have a lot of text.
In theory, you could make sure the height is set to auto, remove the text, and then word by word reinsert it. On a change in the height, you remove the last word, and insert your dom element. Again, this will be slow if there is a lot of text, and the better way to do this would be to not change the original element, but do it in another element which could be hidden, and then replace the original element on completion. That way, your user wouldn't see the content disappear and then come back word by word.
Another way would be to use a similar principle, and start with an empty element of the same size with height auto, and insert a new character and a space until you get a new line. From there, you can use this as an approximation with the above techinique, or you can blindly add up the length of each string until you find a new line, taking into account the width of your dom element. This technique works better with monospaced fonts though, which is where using it only as an approximation comes in.
That said, there is a way to measure text using canvas, but that may be extreme. In theory it would be, create a canvas element, get the context, set all the font properties, and then use the
context.measureText()
method. An example of it use can be found here.不确定您的具体用例是什么,但http://code.google.com/p/hyphenator/< /a> 可能是您问题的解决方案,或者如果您深入研究 hyphenator.js 源代码,您也许能够找到您正在寻找的代码。
Not sure what exactly your use case is but http://code.google.com/p/hyphenator/ may be a solution to your problem or if you dig into the hyphenator.js source code, you may be able to find the code you are looking for.
我认为使用正则表达式会更容易检测——在保持效率的同时更少的代码。
这是对我有用的东西:
我不确定这是否适用于断弦,但它适用于使用 jQuery 检测换行符。
I think it would be easier to detect using a regex -- much less code while retaining efficiency.
Here's something that worked for me:
I'm not sure if this will work with your broken strings, but it works for detecting line-breks with jQuery.