[英]How to build-in array sub-function in my setAttribute function?
I have written a function that automatically inserts an attribute (ie a per case adapted onclick function) in input elements. 我编写了一个函数,该函数会自动在输入元素中插入一个属性(即按情况调整的onclick函数)。 It also makes a few exceptions.
它还有一些例外。 It looks like this, somewhat simplified for clarity reasons:
看起来像这样,出于清晰的原因有所简化:
function insertAttribute() {
var allInputs = document.getElementsByTagName('input');
var allInputsCount = allInputs.length;
var thatInput = null;
for (i = 0; i < allInputsCount; i++) {
thatInput = allInputs[i];
var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
if ((thatInput.name != "A") && (thatInput.name != "B") && (thatInput.name != "C"))
thatInput.setAttribute("onclick",highlightFunction);
}
}
The problem is, there are some 20 exceptions. 问题是,大约有20个例外。 I could expand the
if
line, but I would rather do it with an array. 我可以扩展
if
行,但是我更愿意使用数组。 But how do I do that? 但是我该怎么做呢? I googled
how to use array in javascript function
, and the (two top) results suggest I should do it like this: 我用谷歌搜索了
how to use array in javascript function
,并且(两个顶部)结果建议我应该这样做:
function insertAttribute() {
var allInputs = document.getElementsByTagName('input');
var allInputsCount = allInputs.length;
var thatInput = null;
for (i = 0; i < allInputsCount; i++) {
thatInput = allInputs[i];
var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
var exceptedArray = ["A","B","C"];
if (thatInput.name != exceptedArray)
thatInput.setAttribute("onclick",highlightFunction);
}
}
But that doesn't work -- the attribute is still inserted in the exceptions. 但这是行不通的-该属性仍插入到异常中。 How should it be done?
应该怎么做? I would need a vanilla script solution.
我需要一个香草脚本解决方案。 I'll be happy with a good tutorial, too.
我也会对一个很好的教程感到满意。 As you might have guessed, this is the first time I'm using such an array sub-function.
您可能已经猜到了,这是我第一次使用这样的数组子功能。
The solution offered in the comment, exceptedArray.indexOf(thatInput.name)==-1
worked in most browsers, but not in IE8. 评论中提供的解决方案,
exceptedArray.indexOf(thatInput.name)==-1
于大多数浏览器,但不适用于IE8。 Its script debugger said that it did not support indexOf
. 它的脚本调试器说它不支持
indexOf
。 It does in other contexts, but apparently not in this context. 它在其他情况下也可以,但显然不在此情况下。
In the meantime, I learned how to make a script loop through an array myself. 同时,我自己学习了如何使脚本循环遍历数组。 And this works in all browsers:
这适用于所有浏览器:
var allInputs = document.getElementsByTagName('input');
var allInputsCount = allInputs.length;
var thatInput = null;
for (var i=0; i<allInputsCount; i++) {
thatInput = allInputs[i];
var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
var exceptedNamesArray = ["A","B","C","A4dTInput","A4eTInput"];
var excNamesArrayCount = exceptedNamesArray.length;
var excName = null;
for (var j=0; j<excNamesArrayCount; j++) {
excName = exceptedNamesArray[j];
if (thatInput.name != excName)
thatInput.setAttribute("onclick",highlightFunction);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.