我用
$("#test").html($("#test").text().replace(/([a-zA-Z]+[.,]? ){50}/,
function(x) { return x + "... <p>new content</p> ... "; }));
在50个单词后添加一些内容。在这种情况下,如何保留完整内容的原始换行符?
我试过了
替代jQuery text()函数 https://www.sitepoint.com/jquery-text-function/
但没有成功。
示例:
https://jsfiddle.net/3wkxzpo0/
$("#test").html($("#test").text().replace(/([a-zA-Z]+[.,]? ){50}/, function(x) { return x + "... <p style=\"background:red\">new content</p> ... "; }));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="test">
Hallo, <br /><br />Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et duo dolores et ea rebum. Stet clita kasd gubergren, no sea <a href="/example.php">test</a> takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur <img src="sdfsdf">sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo
<br /><br />
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
<br /><br />
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet.
</div>
答案 0 :(得分:1)
这是一个vanilla JS解决方案,它迭代元素的childNodes,跟踪每个 textNode 中的字数,并在达到某个字数时插入所需的元素。
var test = document.getElementById('test'), childNodes = test.childNodes,
wordCount = 0, newHTML = [], maxWords = 50, toInsert = ' <b>[50]</b> ',
i = 0, node = null, words = null, inserted = false;
for(i=0; i<childNodes.length; i++) {
node = childNodes[i];
if(node.nodeType !== 3) { // not a text node, append
newHTML.push(node.outerHTML);
}
else { // is a text node, append till maxWords
if(inserted) {
newHTML.push(node.textContent);
}
else {
words = node.textContent.replace(/^\s+/g, '').replace(/\s+$/g, '').split(/[\s\t\n+]/g);
words = words.filter(function(_x) {
return _x !== '';
});
if(wordCount + words.length < maxWords) {
newHTML.push(node.textContent);
}
else {
newHTML.push(words.slice(0, maxWords - wordCount).join(' '));
newHTML.push(toInsert);
newHTML.push(words.slice(maxWords - wordCount).join(' '));
inserted = true;
}
wordCount += words.length;
}
}
}
test.innerHTML = newHTML.join('');
答案 1 :(得分:0)
尝试 -
$("#test").html($("#test").html().replace(/([a-zA-Z]+[.,]? ){50}/, function(x) { return x + "... <p style=\"background:red\">new content</p> ... "; }));
答案 2 :(得分:0)
这很有效。 。 。只需将$("#test").text()
更改为$("#test").html()
https://jsfiddle.net/vq8yf9u9/2/
$("#test").html($("#test").html().replace(/([a-zA-Z=\/<>".]+[.,]? +){50}/, function(x) { return x + "... <p style=\"background:red\">new content</p> ... "; }));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="test">
Hallo, <br /><br />Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et duo dolores et ea rebum. Stet clita kasd gubergren, no sea <a href="/example.php">test</a> takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur <img src="sdfsdf">sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo
<br /><br />
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
<br /><br />
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet.
</div>
&#13;