我有一个遗留文档,其中包含一些基于模式的附加标签:
<td class="inputControl">
<input id="OffsetAccountsRequired_R0" type="radio" name="OffsetAccountsRequired_N" value="Y" >Yes
<tla:instruction type="unhide" value="OffSetAccountsApp1"></tla:instruction>
<tla:instruction type="jscript" value="//Irrelevant JavaScript here"></tla:instruction>
</input>
<input id="OffsetAccountsRequired_R1" type="radio" name="OffsetAccountsRequired_N" value="N">No
<tla:instruction type="hide" value="OffSetAccountsApp1~OffSetAccountsAppls1and2"></tla:instruction>
</input>
<input id="OffsetAccountsRequired_R2" type="radio" name="OffsetAccountsRequired_N" value="" checked="true" class="hiddenRadio" onclick="validate_js(this)">
<tla:instruction type="hide" value="OffSetAccountsApp1~OffSetAccountsAppls1and2"></tla:instruction>
</input>
</td>
以及在这些标签上工作的 validate_js(this) 事件调用的一些可怕的 JavaScript:
// Calling line - in this case 'obj' is the input control 'OffsetAccountsRequired_R2'
tlaInstructions = getInstructionTags(obj, 'tla:instruction');
function getInstructionTags(inputID,tagType){
var coltla;
var coltlaArray = new Array();
tagType = tagType.toUpperCase();
var tlaFinished = false;
coltla = inputID.parentNode.childNodes;
for(var i=0;i<coltla.length;i++){
if(coltla[i].nodeName.toUpperCase() == 'INPUT' && coltla[i].id == inputID.id){
for(var j=i;j<coltla.length;j++){
if(coltla[j].nodeName.toUpperCase() == tagType){
coltlaArray[coltlaArray.length] = coltla[j];
}
if(coltla[j].nodeName.toUpperCase() == '/INPUT'){
tlaFinished = true;
break;
}
}
}
if(tlaFinished){
break;
}
}
return coltlaArray;
}
}
我的任务是使这个 JavaScript 达到规范,以便它可以在 IE9 中工作(它以前只在 vb6 网络浏览器控件中使用过)。
就我有限的 JavaScript 技能而言,代码的用途是从输入元素中检索所有 tla:instruction 节点。它使用这条线coltla = inputID.parentNode.childNodes
这看起来很奇怪,因为它上升一个水平,然后下降一个水平。我假设它这样做是因为如果您简单地使用检索 tla:instruction 元素不会检索节点coltla = inputID.getElementsByTagName('tla:instruction')
.
有没有更简单的方法可以从输入控件中检索特定类型的元素数组?