cuốn sách gpt4 ai đã làm

Cách ngắt dòng (span) từ trong trình soạn thảo văn bản

In lại Tác giả: Walker 123 更新时间:2023-11-28 04:49:26 27 4
mua khóa gpt4 Nike

我有一个像这样的 JavaScript 变量:

var text = "A businessman should be able to manage his business matters";

我想将每个单词包装在具有不同 id 的 span 元素中,但保留已包装在 中的单词。标签。像这样:

text = "A businessmanshouldbe able to managehis business matters";

我想要用 javascript 或 jquery 实现所有这些,但无法得到它。如果你们能帮助我,那就太好了。

Cảm ơn.

câu trả lời hay nhất

试试这个,

function removeEmptyStrings(k) {
return k !== '';
}

function getWordsArray(div) {
var rWordBoundary = /[\s\n\t]+/; // split by tab, newline, spaces
đầu ra var = [];
for (var i = 0; i < div.childNodes.length; ++i) { // Iterate through all nodes
var node = div.childNodes[i];
if (node.nodeType === Node.TEXT_NODE) { // The child is a text node
var words = node.nodeValue.split(rWordBoundary).filter(removeEmptyStrings); // check for emptyness
for (var j = 0, l = words.length; j < l; j++) {
// adding class txtSpan so that it will not affect your spans already available in your input.
output.push('' + words[j] + '');
}
} else { // add directly if not a text node
output.push(node.outerHTML);
}
}
return output;
}
var counter = 1,
html = getWordsArray($('#editor')[0]).join('');
$('#output').html(html).find('span.txtSpan').each(function() {
this.id = 'span-' + counter++;
});
span.txtSpan {
phần đệm: 2px;
hiển thị: khối nội tuyến;
text-decoration: underline;
}


A businessman should be able to manage his business matters



关于javascript - 如何在文本编辑器中换行(跨度)单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43063416/

27 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress