gpt4 book ai didi

jquery - 如果H2有多个单词,则用span包裹最后一个单词

转载 作者:行者123 更新时间:2023-12-01 02:14:56 25 4
gpt4 key购买 nike

如果有多个单词,jQuery 是否可以将最后一个单词包含在 h2 标签内。

例如。

如果:

<h2>Example</h2>

总比什么都不做

但是如果:

<h2>This is next example</h2>

而不是用 span 包裹最后一个单词。

<h2>This is next <span>example</span></h2>

最佳答案

$("h2").html(function(){

// separate the text by spaces
var text= $(this).text().split(" ");

// drop the last word and store it in a variable
var last = text.pop();

// join the text back and if it has more than 1 word add the span tag
// to the last word
return text.join(" ") + (text.length > 0 ? " <span>"+last+"</span>" : last);

});

查看演示: http://jsfiddle.net/VHDpT/1/

关于jquery - 如果H2有多个单词,则用span包裹最后一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com