日期:2014-05-16  浏览次数:20405 次

execCommand(CreateLink)的问题
想在iframe插入超链接,但一定要先点一下iframe,再执行插入命令,才能在iframe中插入链接。如果先点一下网页页他部分在插入超链接,那么链接就会出现在整个页面的最上放,而不是iframe里面。这到底怎么解决啊。
一下是一些代码:

<script   type= "text/javascript "   language= "JavaScript ">
var   editor;
function   Init()   {
    editor   =   document.getElementById( "MsgFrame ").contentWindow;
    editor.document.open();
    editor.document.writeln( ' <html> <head> ');
    editor.document.writeln( " </head> <body> </body> </html> ");
    editor.document.close();
    editor.document.designMode   =   'On ';
    editor.document.contentEditable   =   true;
    editor.document.charset= "gb2312 ";
}
</script>

<body   onLoad= "Init(); ">

//   按钮
<div   onclick= "editor.document.execCommand( 'CreateLink '); "   > <img   src= "common/image/url.gif "   width= "20 "   height= "20 "   title= "插入超链接 "   alt= "插入超链接 "   /> </div>

//   编辑框
<iframe   style= "width:   100%;   height:100 "   marginwidth= "0 "   marginheight= "0 "   frameborder= "1 "   id= "MsgFrame "   name= "MsgFrame "   src= "about:blank "> </iframe>

------解决方案--------------------
你可以先 editor.focus()