我正在尝试将以下4个功能重写为一个功能

时间:2020-09-01 07:58:00

标签: javascript

function ingredientsHover() {
  document.getElementById('ingredients').firstElementChild.firstElementChild.style.fontSize = '300%';
}
function ingredientsNormal() {
  document.getElementById('ingredients').firstElementChild.firstElementChild.style.fontSize = '100%';
  // document.write("Normal");  
}
function preparationHover() {
  document.getElementById('preparation').firstElementChild.firstElementChild.style.fontSize = '300%';
}
function preparationNormal() {
  document.getElementById('preparation').firstElementChild.firstElementChild.style.fontSize = '100%';
}

1 个答案:

答案 0 :(得分:0)

创建一个接受元素ID和fontSize值的单个函数。在函数内部,将元素id作为变量传递给document.getElementById

function ingredientsStyling(elem, styleVal) {
  document.getElementById(elem).firstElementChild.firstElementChild.style.fontSize = styleVal;
}
ingredientsStyling('ingredients', '300%');