我正在尝试编写一个设置html元素

时间:2017-03-21 18:08:40

标签: javascript html css

所以我遇到的问题是,当display应该返回时,控制台会说editStyle(list, display, "list-item");未定义:

function(){
    if(list[0]===undefined) {
        list.style.display="list-item"
    }
    else {
        for(var i=0; i<list.length; i++) {
            list[i].style.display="list-item"
        }
    }
}

代码:

var title=document.getElementsByClassName('title');
    var list=document.getElementsByClassName('list');
    var test=document.getElementById("testText");
    function editStyle(element, prop, newVal){
        //returns a function which changes the elements prop to newVal
    return function(){if(element[0]===undefined){element.style.prop=newVal;}
    //should check if prop is a list of elements or a single element                                                
    else {for(var i=0; i<element.length; i++){
    element[i].style.prop=newVal;}
    }};
    };
    function onEvent(element, event, action){
      if(element[0]==undefined){  element.event=action;}
      else{for(var i=0; i<element.length; i++){
          element[i].event=action;
      }}
    }
    onEvent(title, onmouseover, editStyle(list, display,"list-item"));
    //should set the title.onmouseover event to equal the function out put of editStyle 
    // which should change the list.style.display value to "list-item"

这是在精彩评论员的帮助下的工作代码:

function editStyle(element, prop, newVal){
    //returns a function which changes the elements prop to newVal
return function(){if(element[0]===undefined){element.style[prop]=newVal;}
//should check if prop is a list of elements or a single element                                                
else {for(var i=0; i<element.length; i++){
element[i].style[prop]=newVal;}
}};
};
function onEvent(element, event, action){
  if(element[0]==undefined){  element.event=action;}
  else{for(var i=0; i<element.length; i++){
      element[i][event]=action;
  }}
}
onEvent(title, "onmouseover", editStyle(list, "display","list-item"));
//should set the title.onmouseover event to equal the function out put of editStyle 
// which should change the list.style.display value to "list-item"

0 个答案:

没有答案