日期:2014-05-16 浏览次数:20496 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>new document</title>
<script type="text/javascript">
function setColor(){
if(document.all){
var tr = document.selection.createRange();
tr.execCommand("ForeColor", false, "#FF0000");
}else{
var tr = window.getSelection().getRangeAt(0);
var span = document.createElement("span");
span.style.cssText = "color:#ff0000";
tr.surroundContents(span);
}
}
</script>
</head>
<body>
<div>fdjlksafjd;slafjd;slakfjds</div>
<input type="button" onclick="setColor()" value="setColor" />
</body>
</html>
------解决方案--------------------
解决了,使用
document.execCommand("ForeColor", true, "#BBDDCC");
------解决方案--------------------
<select name="" id="btn">
<option value="red">红色</option>
<option value="yellow">黄色</option>
</select>
<textarea id="put"></textarea>
<div id="display">
</div>
<script>
var t = document.getElementById('put');
var btn = document.getElementById('btn').options;
btn.onclick = function(){
var color = this.value ;
if(window.getSelection) {
if( (t.selectionStart != undefined) && (t.selectionEnd != undefined) ) {
var text = t.value.substring(t.selectionStart, t.selectionEnd);
alert(text);
//document.getElementById('display').innerHTML = text ;
//document.getElementById('display').style.color = color ;
} else {
return "";
}
} else {
var text = document.selection.createRange().text ;
document.getElementById('display').innerHTML = text ;
document.getElementById('display').style.color = color ;
}
}
</script>