本文链接
在IE中iframe或者frame的contentWindow属性可以省略,但在Firefox中如果要对iframe对象进行编辑则
必须指定contentWindow属性。
function EnableEdit()
{
var editor;
editor = document.getElementById("HtmlEdit").contentWindow;
// 针对IE浏览器, make it editable
editor.document.designMode = 'On';
editor.document.contentEditable = true;
// For compatible with FireFox, it should open and write something to make it work
editor.document.open();
editor.document.writeln('<html><head>');
editor.document.writeln('<style>body {background: white;font-size:9pt;margin: 2px; padding: 0px;}</style>');
editor.document.writeln('</head><body></body></html>');
editor.document.close();
}
<iframe ID="HtmlEdit" MARGINHEIGHT="1" MARGINWIDTH="1" width="100%" height="312">
</iframe>
<html>
<body>
<script>
var ifr = document.createElement("iframe");
document.body.appendChild(ifr);
var ifrdoc = ifr.contentWindow.document;
var s = fixingHTB.innerHTML; //进入可编辑模式前存好
ifrdoc.designMode = "on"; //文档进入可编辑模式
ifrdoc.open(); //打开流
ifrdoc.write(s);
ifrdoc.close(); //关闭流
ifrdoc.designMode ="off"; //文档进入非可编辑模式
</script>
</body>
frames.contentWindow.TshowMsg(msg,'');
之前曾发过一个js版的俄罗斯方块,界面比较简单,Bug也不少。抽空重构了一下,加入了javaScript面向对象的知识,修复了一些明显的BUG。
斌斌 (给我写信) 原创博文(http://blog.csdn.net/binbinxyz),转载请注明出处!
源码(下载)公开如下:
<!DOCTYPE html> <html> <head> <title>俄罗斯方块_经典游戏</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="keywords" content="游戏, 经典游戏, 俄罗斯方块, tetris"> <meta http-equiv="description" content="斌斌的小游戏:俄罗斯方块"> <link rel="icon" href=/blog_article/"http_/www.ifeng.com/favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href=/blog_article/"http_/www.ifeng.com/favicon.ico" type="image/x-icon" /> <link rel="bookmark" href=/blog_article/"http_/www.ifeng.com/favicon.ico" type="image/x-icon" /> <!-- 游戏名称:俄罗斯方块(Tetris)<br/> 游戏版本:1.1<br/> 作者:斌斌<br/> 邮箱:cn.binbin@qq.com<br/> 网站:http://blog.csdn.net/binbinxyz<br/> --> <style type="text/css"> <!-- .thinBorderTable{border-collapse:collapse;} .thinBorderTable th{background:#ffffff;border:solid 1px #000000;} .thinBorderTable td{background:#ffffff;border:solid 1px #000000;} .outerBorderTable{border-collapse:collapse;} .outerBorderTable th{background:#ffffff;} .outerBorderTable td{background:#ffffff;} --> </style> </head> <body> <div id="game" align="center"> <h2>俄罗斯方块 <span style="font-size: 14px">V1.1</span></h2> <table> <tr> <td> <fieldset> <legend>俄罗斯方块的<b>基本规则</b></legend> <table id="info"> <tr valign="top"><td>1、</td><td width='400px'>一个用于摆放小型正方形的平面虚拟矩形,标准大小:宽为10,高为20,以小正方形为单位。</td></tr> <tr valign="top"><td>2、</td><td width='400px'>一组由4个小型正方形组成的规则图形即方块(Tetromino ),共有7种,分别以S、Z、L、J、I、O、T这7个字母的形状来命名。</td></tr> <tr valign="top"><td>3、</td><td width='400px'>部分游戏有单格方块,可以穿透固定的方块到达最下层空位。</td></tr> <tr valign="top"><td>4、</td><td width='400px'>玩家的操作有:以90度为单位旋转方块,以格子为单位左右移动方块,让方块加速落下。</td></tr> <tr valign="top"><td>5、</td><td width='400px'>方块移到区域最下方或是落到其他方块上会固定在该处,而新的方块从区域上方开始落下。</td></tr> <tr valign="top"><td>6、</td><td width='400px'>当区域中某一行横向格子全部由方块填满,则该行会消失。</td></tr> <tr valign="top"><td>7、</td><td width='400px'>当固定的方块堆到区域最上方而无法消除层数时,则游戏结束。</td></tr> <tr valign="top"><td>8、</td><td width='400px'>游戏会提示下一个要落下的方块。</td></tr> <tr valign="top"><td>9、</td><td width='400px'>计分标准:下落一个方块1分,一次消一行10分、2行30分、3行60分、4行100分。</td></tr> </table> </fieldset> <fieldset> <legend>游戏按键提示</legend> <table style="width: 430px;margin: 0px;padding: 0px"> <tr> <td><b>↑</b>:旋转方块</td> <td><b>↓</b>:方块下移</td> <td><b>←</b>:方块左移</td> <td><b>→</b>:方块右移</td> </tr> <tr> <td colspan="4">空格键(<b>Spacebar</b>):方块降落(直接下移到位)</td> </tr> </table> </fieldset> </td> <td> <fieldset> <legend>显示屏幕</legend> <div id='screen' style="background-color: #aaaaaa;padding: 5px"></div> </fieldset> </td> <td> <div style="width: 200px;"> <fieldset> <legend>显示区域</legend> <table> <tr><td>当前级别:</td><td><span id='level'>1</span></td></tr> <tr><td>当前分数:</td><td><span id='score'>0</span></td></tr> <tr><td>运行时间:</td><td><span id='runTime'>00:00</span></td></tr> <tr><td colspan='2'><div id="smallScreen"></div></td></tr> </table> </fieldset> <fieldset id="configArea" style="display: none;"><!-- style="display: none;" --> <legend>配置区域</legend> <table> <tr><td>显示屏幕辅助线:</td></tr> <tr> <td> <label><input type='radio' id='lineVisible_1' name='lineVisible' value='Y' checked='checked'/>YES</label> <label><input type='radio' id='lineVisible_2' name='lineVisible' value='N'/>NO</label> </td> </tr> <tr><td>生成的方块颜色随机:</td></tr> <tr> <td> <label><input type='radio' name='randomTetrominoColor' checked='checked' onclick='setRTColor(true)'/>YES</label> <label><input type='radio' name='randomTetrominoColor' onclick='setRTColor(false)'/>NO</label> </td> </tr> <tr><td>底部方块的颜色随机:</td></tr> <tr> <td> <label><input type='radio' name='randomBottomColor' checked='checked' onclick='setRBColor(true)'/>YES</label> <label><input type='radio' name='randomBottomColor' onclick='setRBColor(false)'/>NO</label> </td> </tr> </table> </fieldset> <fieldset> <legend>控制区域</legend> <input id='btnstart' type='button' value='开始' onclick="start()"/> <input id='btnpause' type='button' value='暂停' disabled="disabled" onclick='pause()'/> <input id='btnend' type='button' value='结束' disabled="disabled" onclick='end()'/> </fieldset> </div> </td> </tr> </table> </div> <script type="text/javascript"> var COLUMN = 10; //显示屏幕的宽度(相对列数) var ROW = 20; //显示屏幕的高度(相对行数) //定义方块数据数组,存储所有的方块数据 var TETROMINO_DATA = [ [ [[0,0,0,0],[1,1,1,1], [0,0,0,0], [0,0,0,0]], [[0,1,0,0], [0,1,0,0], [0,1,0,0], [0,1,0,0]] ], [ [[1,1,1,0], [1,0,0,0], [0,0,0,0], [0,0,0,0]], [[1,1,0,0], [0,1,0,0], [0,1,0,0], [0,0,0,0]], [[0,0,1,0], [1,1,1,0], [0,0,0,0], [0,0,0,0]], [[0,1,0,0], [0,1,0,0], [0,1,1,0], [0,0,0,0]] ], [ [[0,0,0,0], [1,1,1,0], [0,1,0,0], [0,0,0,0]], [[0,1,0,0], [1,1,0,0], [0,1,0,0], [0,0,0,0]], [[0,1,0,0], [1,1,1,0], [0,0,0,0], [0,0,0,0]], [[0,1,0,0], [0,1,1,0], [0,1,0,0], [0,0,0,0]] ], [ [[0,0,0,0], [1,1,1,0], [0,0,1,0], [0,0,0,0]], [[0,1,0,0], [0,1,0,0], [1,1,0,0], [0,0,0,0]], [[1,0,0,0], [1,1,1,0], [0,0,0,0], [0,0,0,0]], [[0,1,1,0], [0,1,0,0], [0,1,0,0], [0,0,0,0]] ], [ [[0,0,0,0], [1,1,0,0], [0,1,1,0], [0,0,0,0]], [[0,0,1,0], [0,1,1,0], [0,1,0,0], [0,0,0,0]] ], [ [[0,1,0,0], [0,1,1,0], [0,0,1,0], [0,0,0,0]], [[0,0,0,0], [0,1,1,0], [1,1,0,0], [0,0,0,0]] ], [ [[0,0,0,0], [0,1,1,0], [0,1,1,0], [0,0,0,0]] ] ]; var POSSIBLE_COLORS = [ "aqua", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "purple", "red", "silver", "teal", "yellow"]; //可能使用的颜色库 var DEFAULT_TETROMINO_COLOR = "GRAY"; //生成方块的默认颜色 var DEFAULT_BOTTOM_COLOR = "GRAY"; //底部方块的默认颜色 var DEFAULT_BLOCK_X = 4; //方块出现时左上角默认位置的X坐标 var bottom = []; //底部方块堆(实际是与屏幕大小相同的二维数组) var bottomColor = []; //