Javascript 如何通过JS或jQuery操作textarea中选定的文本?
我写了一个关于抓取选定文本的小片段,效果很好。 HTML 我想用高亮显示的test!包装一个p标记Javascript 如何通过JS或jQuery操作textarea中选定的文本?,javascript,jquery,Javascript,Jquery,我写了一个关于抓取选定文本的小片段,效果很好。 HTML 我想用高亮显示的test!包装一个p标记 This is just <p>a test!</p> 我能行 selectedText = "<p>"+selectedText+"</p>" 但是接下来呢?如何将其放入文本区域并保留其他文本 谢谢 表单字段(如textareas)仅显示纯文本,即不会在其中呈现HTML代码。这应该可以: var selectedText = textCompo
This is just
<p>a test!</p>
我能行
selectedText = "<p>"+selectedText+"</p>"
但是接下来呢?如何将其放入文本区域并保留其他文本
谢谢 表单字段(如textareas)仅显示纯文本,即不会在其中呈现HTML代码。这应该可以:
var selectedText = textComponent.value.substring(startPos, endPos);
var precedingText = textComponent.value.substr(0, startPos - 1);
var followingText = textComponent.value.substr(endPos + 1);
textComponent.innerHTML = precedingText
+ "<p>" + selectedText + "</p>"
+ followingText;
但对于浏览器无关的解决方案,您确实应该考虑使用jQuery。这很有趣 首先,p标记是否突出显示文本?如果是这样,那么 答案有很多: DOM对象 var wrapper=document.createElement p wrapper.appendChild textComponent 警报包装器.outerHTML 使用Jquery var wrapper=$textComponent.wrapp[0] 警报包装器.outerHTML 使用简单的JS textComponent.innerHTML=+textComponent.innerHTML+ 警报textComponent.outerHTML
按原样尝试此代码:
$(function () {
$("button").on("click", function () {
var el = document.getElementById("text");
replaceSelectedText(el);
});
});
function getInputSelection(el) {
var start = 0,
end = 0,
normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
function replaceSelectedText(el) {
var sel = getInputSelection(el),
val = el.value;
text = '<p>'+val.slice(sel.start,sel.end)+'</p>';
el.value = val.slice(0, sel.start) + text + val.slice(sel.end);
}
如果您只想在文本区域中添加其他文本,可以执行以下操作:$'myTextarea'.val$'myTextarea'.val+'some additional text'。此外,您可以通过使用选择器包装字符串,将其转换为jQuery元素:$+selectedText+.appendTomyDiv;这不是我想要的,sry
selectedText = "<p>"+selectedText+"</p>"
var selectedText = textComponent.value.substring(startPos, endPos);
var precedingText = textComponent.value.substr(0, startPos - 1);
var followingText = textComponent.value.substr(endPos + 1);
textComponent.innerHTML = precedingText
+ "<p>" + selectedText + "</p>"
+ followingText;
$(function () {
$("button").on("click", function () {
var el = document.getElementById("text");
replaceSelectedText(el);
});
});
function getInputSelection(el) {
var start = 0,
end = 0,
normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
function replaceSelectedText(el) {
var sel = getInputSelection(el),
val = el.value;
text = '<p>'+val.slice(sel.start,sel.end)+'</p>';
el.value = val.slice(0, sel.start) + text + val.slice(sel.end);
}