1. 程式人生 > >KF富文字編輯器

KF富文字編輯器

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>KF富文字編輯器</title>
<script type="text/javascript" src="jquery.min.js">
</script>
<script type="text/javascript">
$(function(){
$d = $("#editor")[0].contentWindow.document; // IE、FF都相容
$d.designMode="on"; //分別設定designMode屬性為’on’,contentEditable屬性為’true’讓iframe可編輯
$d.contentEditable= true;
$d.open(); //但是在實際執行的時候,你是否發現除了chrome瀏覽器外(用IETester, Firefox, Chrome測試)開啟這個頁面都處於正在載入的狀態(那個輪子轉啊轉,轉個不停…)只要在doc.write()方法前後加上doc.open(), doc.close()就可以了(在寫之前開啟,寫完之後關閉)。
$d.close();
// 在iframe中插入一張圖片
$('#insert_img').click(function(){
var img = '<img src="' + $('#path').val() +'" />';
$("body", $d).append(img);
});
// 獲取iframe的body內容,用於顯示或者插入到資料庫
$('#preview').click(function(){
alert($('#editor').contents().find('body').html());
$('#preview_area').html($('#editor').contents().find('body').html());
});

//粗體
$('#bBtn').click(function(){
var win=document.getElementById("editor").contentWindow;
$d.execCommand("Bold",false,null);

});
//字型大小
$('#sss').click(function(){
var win=document.getElementById("editor").contentWindow;
win.document.execCommand("FontSize",false,'18px');
win.focus();
});


});
</script>
</head>
<body>
<p>
<input type="button" id="sss" value="s" style="font-weight:bold" />
</p>
<p>
<input type="button" id="bBtn" value="B" style="font-weight:bold" />
</p>
<p><iframe id="editor" width="600px" height="200px" style="border:solid 1px;"></iframe></p>
<input type="text" id="path" value="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"/>
<input type="button" id="insert_img" value="插入圖片" />
<input type="button" id="preview" value="預覽" />
<p style="border: 1px dashed #ccc;" id="preview_area"></p>
</body>
</html>