javascript - how to make a class accept a method without specifying the method in advance? -


if have :

function foo(class){     for(i=0; i<(class.length);i++){        return document.getelementsbyclassname(class)[i];     } } 

and want this:

 foo("someclass").innerhtml="something"; 

it first element , understand why happens , how make work correctly, how can make function other methods without telling in loop do,like this

   foo("someclass").innerhtml="something";//this put inside element    foo("someclass").style.backgroundcolor="#000");// , work 

so,if possible,how can make function without putting these methods in foo function loop? there way put these methods in variable this

  function foo(class).variablemethod{ for(i=0; i<(class.length);i++){    document.getelementsbyclassname(class)[i].variablemethod; } 

}

is possible?

you can pass function foo(), , have foo() call function each matched element:

function foo(classname, func) {     var elements = document.getelementsbyclassname(classname);     (var = 0; < elements.length; ++i) {         func(elements[i]);     } } 

now can like:

foo("someclass", function(element) {     element.innerhtml = "something";     element.style.backgroundcolor = "#000"; }); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -