在Jquery和Rails,如何获得字符串的可见字符计数? [英] In Jquery AND Rails, how to get visible character count of a string?

查看:126
本文介绍了在Jquery和Rails,如何获得字符串的可见字符计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个包含嵌入链接和标签的字符串:

 此字符串具有< a href = http://www.google.com>一些链接< / a>里面它。 

然后可见的字符串给用户:

 此字符串中有一些链接。 

完整字符串有73个字符,可见只有37个字符。我想要的是能够在jQuery和Rails获得字符计数的字符串计数只是可见字符。 (jQuery,我可以在一个输入字段中给出一个活字符计数,以及用于验证和截断目的的Rails)。



任何人都知道什么可以帮助?当然有一些方法这样做..

解决方案

这只是答案你的问题的一半。



在jQuery中,如果你有一个这样的字符串 -

  var text ='This string has< a href =http://www.google.com>一些链接< / a>里面。'

然后你可以做类似 -

  $('< div>< / div>')。append(text).text()
pre>

这会给你 -


这个字符串里面有一些链接。


因此 $('< div>< / div>') .text()。length 会得到没有html标记的字符串的长度。


Let's say you have a string with embedded links and tags like so:

 This string has <a href="http://www.google.com">some links</a> inside it.

Then the visible string to a user is:

 This string has some links inside it.

The full string has 73 characters and the visible just 37 characters. What I would like is to be able to both in jQuery and in Rails get character counts for strings counting just visible characters. (jQuery such that I could give a live character count in an input field, and Rails for validations and truncation purposes).

Anyone know anything that could help? Surely there is some way of doing this..

解决方案

This only answers the JavaScript half of your question.

In jQuery, if you have a string like this -

var text = 'This string has <a href="http://www.google.com">some links</a> inside it.'

Then you could do something like -

$('<div></div>').append(text).text()

which would give you -

This string has some links inside it.

So $('<div></div>').append(text).text().length would get you the length of the string without the html mark-up.

这篇关于在Jquery和Rails,如何获得字符串的可见字符计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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