选择容器中的最后一个单词 [英] Select last word in a container

查看:82
本文介绍了选择容器中的最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有办法在DIV中选择最后一个WORD。我认为没有任何明显的方法可以做到这一点,所以有什么工作吗?



我不介意使用CSS或Javascript来实现这一点。 / p>

提前致谢

解决方案

试试这个:

  var $ div = $('div'); 
$ div.html($ div.text()。replace(/(\ w +?)$ /,'< span> $ 1< / span>'));

这是一个演示



如果div中的文本不包含任何div元素,那么这将有效。否则,它不会,因为它将用纯文本替换所有以前的元素。


I was just wondering if there is a way to select the last WORD in a DIV. I don't think there is any obvious way to do this, so are there any work arounds?

I don't mind using CSS or Javascript to achieve this.

Thanks in advance

解决方案

Try this:

var $div = $('div');    
$div.html($div.text().replace(/(\w+?)$/, '<span>$1</span>'));

Here is a demo

If the text inside the div does not contain any div element, then this will work. Otherwise, it won't, because it will replace all the previous elements with plain text.

这篇关于选择容器中的最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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