admin管理员组文章数量:1334674
I would like to use jQuery to find specific character, in this case "#" inside one or more paragraphs, remove that character, and wrap everything after in span tag.
Here is HTML example:
<p>Lorem ipsum dolor sit amet # consectetur adipiscing elit<p>
And jQuery should convert code above into this:
<p>Lorem ipsum dolor sit amet <span>consectetur adipiscing elit</span></p>
I spent few hours on this already and I know solution should be relatively simple, but I can't find it. Is there any jQuery guru willing to help? Thanks!
I would like to use jQuery to find specific character, in this case "#" inside one or more paragraphs, remove that character, and wrap everything after in span tag.
Here is HTML example:
<p>Lorem ipsum dolor sit amet # consectetur adipiscing elit<p>
And jQuery should convert code above into this:
<p>Lorem ipsum dolor sit amet <span>consectetur adipiscing elit</span></p>
I spent few hours on this already and I know solution should be relatively simple, but I can't find it. Is there any jQuery guru willing to help? Thanks!
Share Improve this question asked Apr 19, 2011 at 18:44 KlikerkoKlikerko 1,0972 gold badges11 silver badges29 bronze badges4 Answers
Reset to default 4Here is a working jsFiddle demo:
HTML:
<p>Lorem ipsum dolor sit amet # consectetur adipiscing elit</p>
jQuery:
$("p:contains('#')").each(function() {
var $this = $(this),
splitText = $this.text().split("#"),
formattedText = splitText[0] + "<span>" + splitText[1] + "</span>";
$this.html(formattedText);
});
-- In case you wanted to keep nesting spans for multiple occurrences: --
Use this working jsFiddle demo:
HTML:
<p>Lorem ip#sum dolor sit amet # consectetur adip#iscing elit</p>
jQuery:
$("p:contains('#')").each(function() {
var $this = $(this),
thisText = $this.text(),
numberOfOccurrences = thisText.split("#").length,
formattedText = thisText.replace(/\#/g, "<span>");
for (var i = 1; i < numberOfOccurrences; i++) { formattedText += "</span>"; }
$this.html(formattedText);
});
Update Notes:
- Multiple Occurrence jsFiddle was updated to remove the extra var splitText, as it was unnecessary.
- Multiple Occurrence jsFiddle was moved and css was updated to visually show the spans.
Something like this
$("p:contains('#')").each(function () {
var $this = $(this);
var thisText = $(this).text().split("#");
$this.html(thisText[0] + "<span>" + thisText[1] + "</span>";
});
This should work:
$("p").each(function(){
var text = $(this).text().split("|");
$(this).html("");
var final = text[0] + "<span>" + text[1] + "</span>"; $(this).html(final);
});
Here's an example of the following →
$('p').each(function(i,elem){
var $this = $(this),
tStr = $this.text(),
res = tStr.replace(/(^.*)(\#.*$)/, '$1<span>$2</span>').replace('#','');
$this.html(res);
});
版权声明:本文标题:javascript - How to use jQuery to select all text inside paragraph after specific character - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339793a2456366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论