如何使用 javascript 更改输入字段中特定选择文本的 css 属性
How to change css property of the particular selection text in a input field using javascript
这是我的示例演示:
<span style="color: blue; text-decoration: underline;">CHANGE</span>
<form>
<textarea >hello, world</textarea/>
</form>
我想更改所选文本颜色(所选文本将是动态的)输入字段。颜色变化可以根据:
他:红色(如果他选择了)
你好:蓝色(如果选择了你好),依此类推。
单击更改文本后,所选颜色将更改。
您无法在文本区域中执行此操作。任何与文本相关的属性都将影响文本区域中的整个文本。
按照以下方式,您可以动态更改p元素的文本颜色。
对p
标记使用contenteditable="true"
function getText() {
if (window.getSelection) {
return window.getSelection();
} else if (document.selection) {
return document.selection.createRange().text;
}
return '';
}
document.getElementById('btnClick').addEventListener('click', function()
{
if(getText() == "hello")
document.execCommand('foreColor', false, "#ff0000")
else
document.execCommand('foreColor', false, "#0000ff")
});
<button id="btnClick">Change Color</button>
<p id="text1" contenteditable="true">hello, world</p>
这样,您可以检查文本是否为hello,然后将所选文本更改为红色。否则,对于另一个文本,请将其更改为蓝色。
您可以根据需要更改条件。
在此处查看我接受的答案以获取更多详细信息。
使用
contenteditable='true'
而不是textarea
试试这个:
document.getElementById('handler').addEventListener('click', function() {
document.getElementById('red').classList.add('red');
document.getElementById('blue').classList.add('blue');
});
.red {
color: red;
}
.blue {
color: blue;
}
<span style="color: blue; text-decoration: underline;" id='handler'>CHANGE</span>
<form>
<div contenteditable='true'>
<span id='red'>hello, </span>
<span id='blue'>world</span>
</div>
</form>
按照@rayon Dabre的建议更新你的html
.HTML
<span style="color: blue; text-decoration: underline;" id='handler'>CHANGE</span>
<div contenteditable='true'>
<span id='txt'>hello, world</span>
</div>
.JS
document.getElementById('txt').addEventListener('mouseup', function(e){
var selection = (window.getSelection) ? window.getSelection().toString() : document.selection.createRange().text;
this.textContent = this.textContent; //remove child tags before wrap
wrapWithTag(this, selection ? selection : "");
}, false);
var textAndColor = {//Object to update color as per selected text
"he": "red",
"hello": "blue",
"world": "yellow"
};
function wrapWithTag(stringElement, txtToWrap) {
if(!txtToWrap) {return;}
var originalTxt = stringElement.textContent;
var beforeString = originalTxt.substring(originalTxt.indexOf(txtToWrap), 0);
var AfterString = originalTxt.substring(originalTxt.indexOf(txtToWrap) + txtToWrap.length);
if(textAndColor[txtToWrap]) {
stringElement.innerHTML = beforeString + "<span id='highlight' class=' "+ textAndColor[txtToWrap] +"'>" + txtToWrap + "</span>" + AfterString;
}
}
document.getElementById('handler').addEventListener('click', function(e){
var textContainer = document.getElementById('highlight');
if(textContainer) {
textContainer.style.color = textContainer.className;
}
}, false);
相关文章:
- 用于选择/文本框操作的JavaScript
- 以编程方式选择文本Mobile Safari
- Firefox输入可以't在选择文本时滚动
- 如何在javascript中取消选择文本框
- 如何在html中打印选择文本
- SVG元素——处理和选择文本
- 选择文本并添加到本地存储
- 选择“p文本jquery”
- 为什么火狐在我更改 innerHTML 时会选择文本
- 根据换行符选择文本
- 如何按从当前单词到段落中该句子(.)结尾的范围选择文本
- 单击按钮时选择文本
- JavaScript 触发器事件在 Android 上选择文本
- .val() 选择文本而不是值字符串,当网页动态编辑源 html
- 为什么不用特殊字符替换选择文本,以及如何删除此特殊字符
- 使用链接多次选择文本
- 使用jQuery在焦点上选择文本框是't在移动浏览器中工作
- Safari存在文本输入问题,用户输入时会选择文本,导致文本丢失
- Bootstrap下拉菜单-仅复选框不选择文本
- 使用.on的多个下拉菜单选择文本仅适用于第一个下拉菜单