在一定数量的字符后在p内添加一个span标签 [英] Add a span tag inside p after certain amount of characters

查看:115
本文介绍了在一定数量的字符后在p内添加一个span标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您的HTML有了这个

Suppose you have this for your HTML:

<div class="contentBox"> 
    <p>I have some content that is good to read</p>
</div>

但是,您希望在一定数量的字符之后添加一个span标签,假设有26个字符,该字符就在单词"that"之后.结果将如下所示:

However you would like to add a span tag after a certain amount of characters, lets say 26 characters, which would be right after the word "that". The result would look like this:

<div class="contentBox">
    <p>I have some content that<span> is good to read</span></p>
</div>

它必须在一定数量的字符之后,因为该段落会不时更改.

It has to be after a certain amount of characters, because the paragraph will change from time to time.

推荐答案

设置要设置span后的字符数.获取p元素的文本.从开始直到有字符数的子字符串,添加span,继续其余部分,并添加结束的span

Set the amount of characters after you want to set the span. Get text of the p element. Substring from start until the amount of chars, add the span, continue with the rest and add the closing span

尝试:

var after=26;
var html = $(".contentBox p").html();
html = html.substring(0, after) + "<span>" + html.substring(after)+"</span>";
$(".contentBox p").html(html);

演示

DEMO

这篇关于在一定数量的字符后在p内添加一个span标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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