function changeCss(node, selector, property){ node.style.ВОТ СЮДА = property; } |
2 answers
We write the changeCss function, which accepts a selector selector , a CSS property and the value for this property value :
function changeCss(selector, property, value) { var nodes = document.querySelectorAll(selector); for (i = 0; i < nodes.length; ++i) { nodes[i].style[property] = value; } } Call examples
function changeCss(selector, property, value) { var nodes = document.querySelectorAll(selector); for (i = 0; i < nodes.length; ++i) { nodes[i].style[property] = value; } } changeCss("#img", "width", "200px"); changeCss("#img", "height", "200px"); changeCss("#img", "background-image", "url(http://lorempixel.com/output/nature-qc-200-200-6.jpg)"); changeCss(".text", "margin", "10px"); changeCss(".text", "color", "red"); changeCss(".text", "font-size", "50px"); <div id="img"> </div> <div class="text"> This is text </div> |
Use the eval function, which turns a string into code:
function changeCss(node, selector, property) { eval(node+'.style.'+selector+' = "'+property+'"'); }; function changeCss(node, selector, property) { eval(node+'.style.'+selector+' = "'+property+'"'); }; $(function() { changeCss('document.getElementById("text")', 'color', 'red'); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="text">Текст</div> - Oh and naughty. With
evalyou need to be careful, because you can get anything here. - Vadim Ovchinnikov - @VadimOvchinnikov, yes, I am like this: D This is the first thing that came to mind - Yuri
|
nodeargument? - Vadim Ovchinnikov