app.js:31未捕获的DOMException:无法在“元素”上执行“ setAttribute”:“ 0”不是有效的属性名称

时间:2019-08-30 00:54:00

标签: javascript attributes

这是我的app.js

const inputs=document.getElementsByTagName('INPUT');
const select=document.getElementsByTagName("SELECT");


//Menu select elements
const designMenu=select[2];
const colorMenu=select[3];
const colors=colorMenu.children;
const jobRoleMenu=select[0];
const paymentMenu=select[4];
const paymentOptions=paymentMenu.children;


//fieldset 
const fieldSetShirt=document.getElementsByTagName("FIELDSET")[1];
const fieldSetActivity=document.getElementsByTagName("FIELDSET")[2];
const fieldSetPay=document.getElementsByTagName("FIELDSET")[3];



const payPal=document.querySelector(".paypal");
const bitCoin=document.querySelector(".bitcoin");
const creditCardOption=document.querySelector(".credit-card");



//helper functions 

setAttributes=(randomElement, attributes)=> {
  Object.keys(attributes).forEach(name=> {
    randomElement.setAttribute(name, attributes[name]);
  })
}


hidePaymentInfo = (info1,info2) => {
  info1.style.display="none";
  info2.style.display="none";
}

hidePaymentInfo(payPal,bitCoin);

setPaymentSettings= (paymentOptions) => {
  // paymentMenu.children[0].setAttribute("disabled", true);
  // paymentMenu.children[1].setAttribute("selected", true);
  setAttributes(paymentMenu.children[0], "disabled");
  setAttributes(paymentMenu.children[1], "selected");
}


setPaymentSettings(paymentOptions)

setDefaultColor= () => {
  //default option till design picked
  const defaultColorOption=document.createElement("option");
  defaultColorOption.innerHTML="Please select a T-shirt theme";
  defaultColorOption.setAttribute("selected", true)
  defaultColorOption.setAttribute("disabled", true)
  //appending default color
  colorMenu.insertAdjacentElement('afterbegin', defaultColorOption)
  //setting color values to hidden
  Array.from(colors).slice(1,7).forEach(color=> color.setAttribute("hidden", true));

}

我正在尝试重构代码,因为我已经在许多元素上设置了属性,因此它尝试使用 setAttributes 函数

抽象该过程

但是,我不知道为什么说元素:0不是有效的属性名称。我尝试修复它,但不幸的是,没有成功。我怎样才能解决这个问题?

0 个答案:

没有答案