HTML编辑器

<button onclick="weight()">加粗</button>
<input type="color" id="e" onchange="changeColor()" />
<input type="file" id ="f" onchange="addPicture()" />
<br/> <br/>
<div contenteditable id="d1" style="width:900px;height:500px;border:1px solid red;overflow:auto;">

</div>

<script>
function weight(){
var range = window.getSelection().getRangeAt(0);
var span = document.createElement("span");
span.style.fontWeight = 900;
range.surroundContents(span);
}

function changeColor(){
var range = window.getSelection().getRangeAt(0);
var span = document.createElement("span");
span.style.color = e.value;
range.surroundContents(span);
}

function addPicture(){
var range = window.getSelection().getRangeAt(0);
var picture = document.createElement("img");
var fileReader = new FileReader();
fileReader.readAsDataURL(f.files[0]);
fileReader.onload = function(){
picture.src = this.result;
range.insertNode(picture);
}
}
</script>

 

posted @ 2019-11-26 21:19  欧阳少璟  阅读(146)  评论(0编辑  收藏  举报