//----------------------------------------------------------- //得到用户输入的单词 function getWord() { //只有当在单词输入框中按回车或 //按“保存”按钮时才继续做下去 if (window.event.type != "keypress" && window.event.type != "click") return; else if (window.event.type == "keypress" && window.event.keyCode != 13) return;
var eleSrc; var iIndex, iNxtWrdIdx; var strWord;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //iNxtWrdIdx :新单词在单词数组中的索引。整型 //strWord :用户输入的单词。字符串型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc); //获得索引 strWord = trim(txtWord[iIndex].value); //获得用户输入的单词
//检测输入的单词是否合法 //若是非法的单词则退出 if (strWord == "") { //若输入的单词为空或空格字符串 alert("单词不能为空或空格字符串"); txtWord[iIndex].select(); return; }
if (strWord.indexOf(",") != -1 || strWord.indexOf(" ") != -1) { //若输入的单词包含逗号或空格 alert("单词不能包含逗号或空格"); txtWord[iIndex].select(); return; }
//将单词保存到词汇数组中 iNxtWrdIdx = m_astrWords[iIndex].length; m_astrWords[iIndex][iNxtWrdIdx] = strWord; //showArray(m_astrWords[iIndex]);
//将单词写入到页面中 valueList(m_astrWords[iIndex], iIndex); txtWord[iIndex].value = ""; txtWord[iIndex].focus(); }
//----------------------------------------------------------- //将当前类别所有的单词从内存中删去 function reset() { var eleSrc; var iIndex, iCount; var strPrompt;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //iCount :计数器。整型 //strPrompt :操作提示。字符串型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc); strPrompt = "您确定要从内存中清除[" + m_astrSenPart[iIndex] + "]部分所有的单词吗?"; if (window.confirm(strPrompt) == false) return;
m_astrWords[iIndex] = new Array(); valueList(m_astrWords[iIndex], iIndex); //showArray(m_astrWords[iIndex]);
strPrompt = "单词清除完毕"; alert(strPrompt); txtWord[iIndex].select(); }
//----------------------------------------------------------- //将所选类别的当前词汇保存进文件中 function save() { var eleSrc; var iIndex, iCount;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //iCount :计数器。整型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc);
if (m_astrWords[iIndex].length == 0) { //如果所选类别的当前单词个数为零 var strPrompt; strPrompt = "[" + m_astrSenPart[iIndex] + "]部分现在一个可选单词也没有,您确定要保存吗?"; strPrompt += "\n(这样将生成一个空的新文件)"; if (window.confirm(strPrompt) == false) return; }
//保存当前内存中所有的单词 for (iCount = 0; iCount < m_iPartCnt; iCount ++) eval("frmWords.txtWords" + iCount).value = m_astrWords[iCount].join(",");
frmWords.txtSenPart.value = iIndex; frmWords.txtOpWay.value = "SAVE"; frmWords.submit(); }
//----------------------------------------------------------- //从文件中读取所选类别的单词纪录 function load() { var eleSrc; var iIndex, iCount;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //iCount :计数器。整型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc);
//保存当前内存中所有的单词 for (iCount = 0; iCount < m_iPartCnt; iCount ++) eval("frmWords.txtWords" + iCount).value = m_astrWords[iCount].join(",")
frmWords.txtSenPart.value = iIndex; frmWords.txtOpWay.value = "LOAD"; frmWords.submit(); }
//----------------------------------------------------------- //显示词汇列表 function showList(iIndex) { //如果未给定参数 iIndex //获得 iIndex if (iIndex == undefined) { //如果已有单词列表被固定,退出函数 if (m_iListFixed != -1) return;
var eleSrc; var iIndex;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc); } //alert("iIndex: "+iIndex);
//显示词汇列表 //lblWordList[iIndex].innerText = "[" + lblWordList[iIndex].innerText + "]"; //lblWordList[iIndex].innerText = lblWordList[iIndex].innerText + ">"; lblWordList[iIndex].className = "lblMOver"; divWordList[iIndex].className= "listShown"; }
//----------------------------------------------------------- //隐藏词汇列表 function hideList(iIndex) { //如果未给定参数 iIndex //获得 iIndex if (iIndex == undefined) { //如果已有单词列表被固定,退出函数 if (m_iListFixed != -1) return;
var eleSrc; var iIndex;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc); } //alert("iIndex: "+iIndex);
//隐藏词汇列表 //lblWordList[iIndex].innerText = lblWordList[iIndex].innerText.slice(0, -1); lblWordList[iIndex].className = "lblMOut"; divWordList[iIndex].className= "listHidden"; }
//----------------------------------------------------------- //固定词汇列表 function fixList() { var eleSrc; var iIndex;
//------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //iIndex :表示词汇分类的数字。整型 //------------------------------------------
eleSrc = window.event.srcElement; iIndex = getEleIndex(eleSrc);
switch (m_iListFixed) { case -1: //如果还没有单词列表被固定 //固定当前列表 m_iListFixed = iIndex; lblWordList[iIndex].className = "lblClicked"; divWordList[iIndex].className= "listFixed"; break;
case iIndex: //如果被固定单词列表是当前列表 //解固当前列表 m_iListFixed = -1; lblWordList[iIndex].className = "lblMOver"; divWordList[iIndex].className= "listShown"; break;
default: //如果被固定单词列表不是当前列表 //解固被固定列表 hideList(m_iListFixed); m_iListFixed = -1; lblWordList[iIndex].className = "lblMOver"; showList(iIndex); break; }
//alert("m_iListFixed: " + m_iListFixed + "\niIndex: " + iIndex); }
//----------------------------------------------------------- //写入某类词汇已载入的单词 function valueList(aWords, iSenPart) { var iCount, iWrdCnt; var strListPpt;
//------------------------------------------ //常变量说明 //iCount :计数器。整型 //iWrdCnt :某类词汇单词的数目。整型 //strListPpt :单词列表中的提示。字符串型 //------------------------------------------
//数据准备 iWrdCnt = aWords.length; lblWordCount[iSenPart].innerText = iWrdCnt; strListPpt = "<div><span style='color: RED; font-style: Italic; font-size: 10.5pt;'>"; strListPpt += "双击单词将其删除</span></div>";
//将单词写入列表 if (iWrdCnt != 0) { divWordList[iSenPart].innerHTML = strListPpt; for (iCount = 0; iCount < iWrdCnt; iCount ++) divWordList[iSenPart].innerHTML += makeOpt(aWords[iCount], iCount); } else divWordList[iSenPart].innerHTML = strListPpt; }
//----------------------------------------------------------- //显示帮助列表 function showHelp() { //如果帮助列表已被固定,退出函数 if (m_iHelpFixed == 0) return;
//显示帮助 lblHelp.className = "lblMOver"; divHelp.className = "helpShown"; }
//----------------------------------------------------------- //隐藏帮助列表 function hideHelp() { //如果帮助列表已被固定,退出函数 if (m_iHelpFixed == 0) return;
//隐藏帮助 lblHelp.className = "lblMOut"; divHelp.className = "helpHidden"; }
//----------------------------------------------------------- //固定帮助列表 function fixHelp() { if (m_iHelpFixed == -1) { //如果帮助列表还未被固定 //固定它 m_iHelpFixed = 0; lblHelp.className = "lblClicked"; divHelp.className = "helpFixed"; } else { //如果帮助列表已被固定 //解固它 m_iHelpFixed = -1; lblHelp.className = "lblMOver"; divHelp.className = "helpShown"; } }
//----------------------------------------------------------- //重置页面至初始状态 function resetAll() { var iCount; //------------------------------------------ //常变量说明 //iCount :计数器。整型 //------------------------------------------
for (iCount = 0; iCount < m_iPartCnt; iCount ++) { txtWord[iCount].value= ""; m_astrWords[iCount] = new Array(); //将单词数组清空 valueList(m_astrWords[iCount], iCount); }
txtSentence.value = ""; txtWord[0].focus(); }
//----------------------------------------------------------- //把所有类别的单词都存入文件 function saveAll() { var iCount, iEmptyCnt; var strPrompt;
//------------------------------------------ //常变量说明 //iCount :计数器。整型 //iEmptyCnt :单词数目为0的类别个数。整型 //strPrompt :操作提示。字符串型 //------------------------------------------
iEmptyCnt = 0; for (iCount = 0; iCount < m_iPartCnt; iCount ++) { //保存当前内存中所有的单词 eval("frmWords.txtWords" + iCount).value = m_astrWords[iCount].join(","); if (m_astrWords[iCount].length == 0) iEmptyCnt ++; } //alert("iEmptyCnt: " + iEmptyCnt);
if (iEmptyCnt > 0) { switch (iEmptyCnt) { case m_iPartCnt: strPrompt = "所有类别的词汇单词个数都为0,操作将会产生" + m_iPartCnt + "个空文件"; strPrompt += "\n您确定要这样做吗?"; break;
default: strPrompt = "其中,"; for (iCount = 0; iCount < m_iPartCnt; iCount ++) if (m_astrWords[iCount].length == 0) strPrompt += "[" + m_astrSenPart[iCount] + "]、"; strPrompt = strPrompt.slice(0, -1); strPrompt += "部分单词个数为0"; strPrompt += "\n操作将会产生" + iEmptyCnt + "个空文件"; strPrompt += ",您确定要这样做吗?"; }
if (window.confirm(strPrompt) == false) return; }
frmWords.txtOpWay.value = "SAVEALL"; frmWords.submit(); }
//----------------------------------------------------------- //从文件中载入所有类别的单词 function loadAll() { var iCount; //------------------------------------------ //常变量说明 //iCount :计数器。整型 //------------------------------------------
//保存当前内存中所有的单词 for (iCount = 0; iCount < m_iPartCnt; iCount ++) eval("frmWords.txtWords" + iCount).value = m_astrWords[iCount].join(",")
frmWords.txtOpWay.value = "LOADALL"; frmWords.submit(); }
//----------------------------------------------------------- //进行词汇搭配,得到一个句子 function assemble() { var iCount, iWrdCnt, iRndIndex; var strSentence;
//------------------------------------------ //常变量说明 //iCount :计数器。整型 //iWrdCnt :某类词汇单词的个数。整型 //iRndIndex :随机产生的单词索引。整型 //strSentence :由各个类别词汇中的随机单词组成的句子。字符串型 //------------------------------------------
strSentence = ""; for (iCount = 0; iCount < m_iPartCnt; iCount ++) { //如果当前类别词汇的个数为0 //给出提示,并中止搭配 iWrdCnt = m_astrWords[iCount].length; if (iWrdCnt == 0) { var strPrompt; //------------------------------------------ //常变量说明 //strPrompt :操作提示。字符串型 //------------------------------------------
strPrompt = "[" + m_astrSenPart[iCount] + "]部分一个可选单词也没有"; strPrompt += "\n请至少输入一个单词"; alert(strPrompt); txtWord[iCount].select(); return; }
//随机抽取当前类别词汇中的一个单词 //加入到句子中 iRndIndex = Math.floor(Math.random()*iWrdCnt); strSentence += m_astrWords[iCount][iRndIndex]; //alert("iWrdCnt: " + iWrdCnt + "\niRndIndex: " + iRndIndex); }
strSentence += "。"; txtSentence.value = strSentence; }
//使鼠标移到之处的按钮 //产生“点亮”的效果 function lightBtn() { var eleSrc; //------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //------------------------------------------
eleSrc = window.event.srcElement; while (eleSrc.tagName.toUpperCase() != "BUTTON") eleSrc = eleSrc.parentElement; eleSrc.className = "btnLighted"; }
//使鼠标离开的按钮 //还原为初始状态 function delightBtn() { var eleSrc; //------------------------------------------ //常变量说明 //eleSrc :发生事件的元素 //------------------------------------------
eleSrc = window.event.srcElement; while (eleSrc.tagName.toUpperCase() != "BUTTON") eleSrc = eleSrc.parentElement; eleSrc.className = "btnDelighted"; } | |