style a div using escaped URL parameters

后端 未结 2 1863
清酒与你
清酒与你 2021-01-26 13:30

I\'m new in javascript and i don\'t exactly understand how to modify this code to style a div:

function getParameters() {
  var searchString = document.getElemen         


        
2条回答
  •  广开言路
    2021-01-26 14:03

    the variables in the .each(function(){}); function are not custom, they are always id and value.

    your function should go like this:

    $.each(hash, function (id, value) {
        test_div.style[id] = value;
    });
    

    here is a FIDDLE

    whats going on basically is, that your hash array contains two objects:

    1) color {value = "red"}

    2) background {value = "yellow}

    the each function iterates thought those, the id will be first time color ad second time background

    and the value will be first time "red" ad second time "yellow"

    so for the first iteration for example, id = 'color' and value ='red' and it will produce:

    test_div.style["color"] = "red";
    

提交回复
热议问题