<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="../base/jquery.js"></script>
<style>
.container {
width: 200px;
height: 200px;
border: 1px solid gray;
}
</style>
</head>
<body>
<div id="J_simpleEditor">
<a class="J_tool">斜体</a>
<div class="container J_container" contenteditable="true">
aaa
</div>
</div>
<script>
(function ($) {
$.fn.simpleEditor = function () {
var that = this;
this.find(".J_container").on("mouseup keyup mouseout", function () {
var sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
that.selectedRange = sel.getRangeAt(0);
}
})
this.find(".J_tool").click(function () {
var selection = window.getSelection();
if (that.selectedRange) {
selection.removeAllRanges();
selection.addRange(that.selectedRange);
}
document.execCommand("Italic");
})
}
})(window.jQuery)
</script>
<script>
$(function () {
$("#J_simpleEditor").simpleEditor();
})
</script>
</body>
</html>