作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,假设我想突出显示文本区域字段中的文本 block ,然后用 <highlight>...</highlight>
将文本括起来。通过按链接来添加标签(例如 <highlight>(highlighted text appears here)</highlight>
)——例如:
<textarea id="comment-body"></textarea>
<a href="#" class="highlight-words" onclick="highlightWords()">Highlight</a>
function highlightWords(){
// code for enclosing highlighted text in textarea appears here
}
有什么想法吗?
最佳答案
您可以使用 JavaScript selectionStart
和 selectionEnd
属性来查找所选文本。这将为您提供开始和结束索引,您可以使用它们来更新字符串。
<textarea id="comment-body"></textarea>
<a href="#" class="highlight-words">Highlight</a>
$('a.highlight-words').click(function() {
var textComponent = document.getElementById('comment-body');
var fullText = document.getElementById('comment-body').value;
var selectedText;
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos)
if (selectedText.length > 0) {
var newStr = fullText.substr(0, endPos) + '</highlight>' + fullText.substr(endPos);
newStr = newStr.substr(0, startPos) + '<highlight>' + newStr.substr(startPos);
textComponent.value = newStr;
}
});
关于javascript - 如何使用 javascript 将突出显示的单词包含在带有一些标签的文本区域中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31513895/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!