简单插入图片在线编辑器代码
2008-08-20 17:22
453 查看
简单的实现插入图片的在线编辑器,如果要测试,需要更改函数insImg()中的变量imgPath的值,上传文件的控件只起到获取文件名的作用,具体路径由自己来设置。
当然,在插入图片之前需要上传本地文件到服务器~
<html>
<head>
<title>editer </title>
<style>
body {
font-size: 12px;
}
#edit, #view {
border: 1px solid red;
width: 100%;
height: 200px;
overflow-y: auto;
margin-bottom: 10px;
}
#view {
border-color: #ddd;
}
</style>
</head>
<body>
<form name="myForm" enctype="mulitipart/form-data" method="post" action="#">
<IFRAME id="edit">
</IFRAME>
<Script Language="JavaScript">
var editor;
editor = document.getElementById("edit").contentWindow;
editor.document.designMode = 'On';
editor.document.contentEditable = true;
editor.document.open();
editor.document.writeln('<html><body></body></html>');
editor.document.close();
function getData(){
alert("getting data");
var value = editor.document.body.innerHTML;
alert("the value is " + value);
document.getElementById("view").value = value;
}
function insImg(){
var file = document.getElementById('myFile');
var fileURL = file.value;
fileURLSplit = fileURL.split(".");
fileExt = fileURLSplit[fileURLSplit.length - 1].toLowerCase();
if (fileExt == "jpg" || fileExt == "gif" || fileExt == "bmp") {
var imgPath = 'http://192.168.0.3/test/img/' + fileURL;
var obj = document.getElementById("edit").contentWindow
obj.document.execCommand('InsertImage', false, imgPath);
}
else {
alert("the format is not correct!" + fileURL);
}
}
</script>
<input type="hidden" id="view" name="desc">
</input>
<br>
<input type="file" id="myFile">
<br>
<input type="button" value="insPic" onclick="insImg()"><input type="button" value="getDate" onclick="getData()">
</form>
</body>
</html>
当然,在插入图片之前需要上传本地文件到服务器~
<html>
<head>
<title>editer </title>
<style>
body {
font-size: 12px;
}
#edit, #view {
border: 1px solid red;
width: 100%;
height: 200px;
overflow-y: auto;
margin-bottom: 10px;
}
#view {
border-color: #ddd;
}
</style>
</head>
<body>
<form name="myForm" enctype="mulitipart/form-data" method="post" action="#">
<IFRAME id="edit">
</IFRAME>
<Script Language="JavaScript">
var editor;
editor = document.getElementById("edit").contentWindow;
editor.document.designMode = 'On';
editor.document.contentEditable = true;
editor.document.open();
editor.document.writeln('<html><body></body></html>');
editor.document.close();
function getData(){
alert("getting data");
var value = editor.document.body.innerHTML;
alert("the value is " + value);
document.getElementById("view").value = value;
}
function insImg(){
var file = document.getElementById('myFile');
var fileURL = file.value;
fileURLSplit = fileURL.split(".");
fileExt = fileURLSplit[fileURLSplit.length - 1].toLowerCase();
if (fileExt == "jpg" || fileExt == "gif" || fileExt == "bmp") {
var imgPath = 'http://192.168.0.3/test/img/' + fileURL;
var obj = document.getElementById("edit").contentWindow
obj.document.execCommand('InsertImage', false, imgPath);
}
else {
alert("the format is not correct!" + fileURL);
}
}
</script>
<input type="hidden" id="view" name="desc">
</input>
<br>
<input type="file" id="myFile">
<br>
<input type="button" value="insPic" onclick="insImg()"><input type="button" value="getDate" onclick="getData()">
</form>
</body>
</html>
相关文章推荐
- 更简单的 编辑器从光标处插入图片(失去焦点后仍然可以在原位置插入)
- 一款支持插入表情的编辑器实现代码(简单思路挺重要)
- 做简单的插入图片代码 PHP+Mysql
- 在线JS编辑器,可运行保存简单JS代码(已开源)
- 制作WEB在线编辑器-插入HTML标签
- 一个简单的javascript图片放大效果代码
- JQuery悬停控制图片轮播――代码简单
- c++ curl 的简单使用和从网页上抓取图片的代码
- java获取指定地址图片高度宽度简单代码
- Python实现简单网页图片抓取完整代码实例
- 20个最强的基于浏览器的在线代码编辑器 - OPEN资讯
- 【代码】微信小程序 简单一个页面,网上图片展示
- OkHttpClient加载图片给DisposableObserver,CompositeDisposable调度控制后台任务队列简单代码
- VC 6.0画简单的ICON图片,并将ICON图片插入到LlistCtrl控件的第一行第一列中
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- c#简单数据库插入操作代码
- 在WORD文档里每一页的相同位置插入不同的图片的宏代码如何修改?
- 使用 CodeMirror 打造属于自己的在线代码编辑器
- 简单的servlet,在线生成QR二维码图片
- 用简单的代码让一组静态图片变成gif动画