|
蓝森林 http://www.lslnet.com 2006年8月22日 18:28
.selection.type为‘Control’的时候..
[html]
<!-- episome -->
<html>
<head>
<title> new document </title>
<meta http-equiv=content-type content="text/html" charset=gb2312>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<body>
<iframe id='Editor' name='Editor'></iframe>
<script language="JavaScript">
initHtml="<img src=''>"
Editor=document.getElementById("Editor");
Editor.document.designMode="On";
Editor.document.open();
Editor.document.write(initHtml);//
Editor.document.close();
function img(){
return_value= '<img border="0" src="http://www.blueidea.com/ad/flashempire/flashempirelogo.gif">'
Editor.focus()
edit = Editor.document.selection.createRange();
RangeType = Editor.document.selection.type;
if (RangeType != "Control"){
edit.select()
edit.pasteHTML(return_value)
}else{
//这里有个问题,当鼠标点中图片(不是select)就不可以插入
edit.pasteHTML(return_value)
}
Editor.focus()
}
function doFormat(what) {
Editor.document.execCommand(what, arguments[1]);
Editor.focus();
}
</script>
<button onclick="img()">Insert Image</button>
</body>
</html>
[/html] |
换一种思路
[html]
<iframe id='Editor' name='Editor'></iframe>
<script language="JavaScript">
initHtml="<img src=''>"
Editor=document.getElementById("Editor");
Editor.document.designMode="On";
Editor.document.open();
Editor.document.write(initHtml);//
Editor.document.close();
function img(){
return_value= '<img border="0" src="http://www.blueidea.com/ad/flashempire/flashempirelogo.gif">'
Editor.focus()
edit = Editor.document.selection.createRange();
RangeType = Editor.document.selection.type;
if (RangeType == "Control"){
Editor.focus();
Editor.document.execCommand('cut','',null)
img()
}else{
//这里有个问题,当鼠标点中图片(不是select)就不可以插入
edit.pasteHTML(return_value)
}
Editor.focus()
}
function doFormat(what) {
Editor.document.execCommand(what, arguments[1]);
Editor.focus();
}
</script>
<button onclick="img()">Insert Image</button>
[/html]
|
你把他Cut掉了,虽然解决了,但是如果可以取消点击状态,把光标移到后面
就好了,我查看了好多文档,都没有看到过,(有也许也看不懂)
3QU。 Ps:佛山无影脚真是厉害,恭喜升官 |
返回的是一个control Range的集合,当然不行了
edit(0).insertAdjacentHTML('afterEnd',return_value)
edit(0).removeNode(); |
不明白,还是糊涂,请myhyli说得详细一点,谢谢 |
var newImg=Editor.document.createElement('IMG');
newImg.src='http://www.blueidea.com/ad/flashempire/flashempirelogo.gif';
edit(0).insertAdjacentElement('afterEnd',newImg);
edit(0).removeNode();
var newRange=Editor.document.selection.createRange();
newRange.moveToElementText(newImg);
newRange.collapse(false);
newRange.select();
这是光标定位在后面的写法 :D |
我用的是DOM的方法
因为返回的是一个集合,所以用(0)来访问第一个成员
|
太高僧了,我要以后才看得懂,不过用佛山人的办法,修改一下就得到这个效果了
[html]
<iframe id='Editor' name='Editor'></iframe>
<script language="JavaScript">
initHtml="<img src=''>"
Editor=document.getElementById("Editor");
Editor.document.designMode="On";
Editor.document.open();
Editor.document.write(initHtml);//
Editor.document.close();
function img(){
return_value= '<img border="0" src="http://www.blueidea.com/ad/flashempire/flashempirelogo.gif">'
Editor.focus()
edit = Editor.document.selection.createRange();
RangeType = Editor.document.selection.type;
if (RangeType == "Control"){
Editor.focus();
Editor.document.execCommand('cut','',null)
Editor.document.execCommand('Paste','',null)
img()
}else{
//这里有个问题,当鼠标点中图片(不是select)就不可以插入
edit.pasteHTML(return_value)
}
Editor.focus()
}
function doFormat(what) {
Editor.document.execCommand(what, arguments[1]);
Editor.focus();
}
</script>
<button onclick="img()">Insert Image</button>
[/html]
我把佛山人Cut掉的再Paste上去 |
啊,你是要插入,不替换原来的图片? |
是啊,如果我要替换,用佛山人那个办法也可以的,呵呵
怪我没有说清楚,Sorry |
那这样
[html]
<!-- episome -->
<html>
<head>
<title> new document </title>
<meta http-equiv=content-type content="text/html" charset=gb2312>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<body>
<iframe id='Editor' name='Editor'></iframe>
<script language="JavaScript">
initHtml="<img src=''>"
Editor=document.getElementById("Editor");
Editor.document.designMode="On";
Editor.document.open();
Editor.document.write(initHtml);//
Editor.document.close();
function img(){
return_value= '<img border="0" src="http://www.blueidea.com/ad/flashempire/flashempirelogo.gif">'
Editor.focus()
edit = Editor.document.selection.createRange();
RangeType = Editor.document.selection.type;
if (RangeType != "Control"){
edit.select()
edit.collapse(false);
edit.pasteHTML(return_value)
}else{
//这里有个问题,当鼠标点中图片(不是select)就不可以插入
var newImg=Editor.document.createElement('IMG');
newImg.src='http://www.blueidea.com/ad/flashempire/flashempirelogo.gif';
edit(0).insertAdjacentElement('afterEnd',newImg);
Editor.document.selection.empty();
var newRange=Editor.document.selection.createRange();
newRange.moveToElementText(newImg);
newRange.collapse(false);
newRange.select();
}
Editor.focus()
}
function doFormat(what) {
Editor.document.execCommand(what, arguments[1]);
Editor.focus();
}
</script>
<button onclick="img()">Insert Image</button>
</body>
</html>
[/html]
|
|