escaped URL parameters statements if else switch

后端 未结 2 602
挽巷
挽巷 2021-01-27 06:07

There is a little problem with this code:

function getParameters() {
  var searchString = document.getElementById(\'input1\').value,
      params = searchString.         


        
2条回答
  •  耶瑟儿~
    2021-01-27 06:54

    in your line

    if(val[0] == "class")
    

    you are only checking the first element in your val array.

    what you would want to do, is iterate through all the hash objects and simply check the attribute like this:

    function getParameters() {
      var searchString = document.getElementById('input1').value,
          params = searchString.split("&"),
          hash = {};
    
      if (searchString == "") return {};
      for (var i = 0; i < params.length; i++) {
        var val = params[i].split("=");
        hash[unescape(val[0])] = unescape(val[1]);
      }
        console.log(hash);
      //return hash;
        $.each(hash, function( attribute, value ) {
    
            if(attribute=="color"){
                test_div.style[attribute]=value;
            }
            else if(attribute=="src"){
                alert(attribute);
                test_div.setAttribute(attribute,value);
            }
        });
    }
    

    here is a working FIDDLE

提交回复
热议问题