简体   繁体   English

我怎样才能抽象出这个改变 CSS 样式的 function

[英]How can I abstract this CSS-style-changing function

I'm trying to abstract a function.我正在尝试抽象一个 function。

This function loops over every child node of a given DOM element (passed into the function as an argument) and applies the same CSS style property & value to each one.此 function 循环遍历给定 DOM 元素的每个子节点(作为参数传递到 function)并将相同的 CSS 样式应用于每个属性和值。

eg例如

function styleChildNodes(parent){

const children = parent.childNodes


for (let i = 0; i < children.length; i++) {

const child = children[i];




child.style.background = "red"


} }

In this example I have hard-coded the CSS property: background & its value as: "red" .在此示例中,我将 CSS 属性:背景及其值硬编码为: “红色” The function will loop over every child node of the given parent element and change their CSS background properties to the color red. function 将遍历给定父元素的每个子节点,并将其 CSS 背景属性更改为红色。

But instead of having any hard-coded CSS property-value pair inside the function such as:但不是在 function 中包含任何硬编码的 CSS 属性值对,例如:

background = "red" , or, visibility = "hidden" , or, opacity = "1" , etc. background = "red" ,或者, visibility = "hidden" ,或者, opacity = "1" ,等等。

I want to pass in the desired CSS property and its value as arguments.我想传入所需的 CSS 属性及其值为 arguments。

Here is an illustration of the concept:以下是该概念的说明:

function styleChildNodes(parent, property, value){

const children = parent.childNodes



for (let i = 0; i < children.length; i++) {

const child = children[i];




child.style.property = value


} }

The CSS property and its value could be anything I choose and I could use the function like this: CSS 属性及其值可以是我选择的任何值,我可以像这样使用 function:

styleChildNodes(div, opacity, "0")

styleChildNodes(table, visibility, "hidden")

styleChildNodes(tr, background, "red")

styleChildNodes(div, height, "10px")

These are just pseudo-coded examples - but hopefully it conveys what I'm trying to achieve.这些只是伪代码示例 - 但希望它传达了我想要实现的目标。

Any ideas, work-arounds, or non-eval() solutions are welcome!欢迎任何想法、变通方法或非 eval() 解决方案!

Thank you for reading:-)感谢您阅读:-)

PS I hope I'm not misapplying "to abstract" when I say "I'm trying to abstract a function." PS 我希望当我说“我正在尝试抽象 function”时,我没有误用“抽象”。 Please let me know if I'm using imprecise terminology!如果我使用不精确的术语,请告诉我!

"Abstraction is a computer science concept in which an implementation is separated from its interface." “抽象是一种计算机科学概念,其中实现与其接口分离。”

You can wrap property in brackets, but the property will need to be passed as a STRING so wrap it in quotes.您可以将属性括在括号中,但该属性需要作为字符串传递,因此请将其括在引号中。

function styleChildNodes(parent, property, value) {
  const children = parent.childNodes
  for (let i = 0; i < children.length; i++) {
    const child = children[i];
    child.style[property] = value
  }
}

you can also pass an object to do more than one:你也可以通过一个 object 来做不止一个:

function styleChildNodes(parent, styles) {
  const children = parent.childNodes
  for (let i = 0; i < children.length; i++) {
    const child = children[i];
    child.style[property] = value
    for(style in styles){
        child.style[style] = styles[style]
   }
  }
}

styleChildNodes(parent, {
"color": "red",
"background-color": "green"
}) 

I read the stylesheet, I created a rule with the external properties in a class called .yourClass and added this rule to the stylesheet.我阅读了样式表,在名为.yourClass的 class 中创建了一个带有外部属性的规则,并将此规则添加到样式表中。 After I have added this class to each node child (also deep node childes in my code).在我将这个 class 添加到每个节点子节点之后(在我的代码中也是深节点子节点)。 I created a possible call and use of this function.我创建了一个可能的调用和使用这个 function。 If you don't understand something please ask.如果您有不明白的地方,请询问。

 let sheet = document.styleSheets[0] function styleChildNodes(parent,...cssPropertiesValues){ let formattedCssPropertiesValues = cssPropertiesValues.map((item,index)=> index%2? `${item};`: `${item}: ` ); let rule = `.yourClass { ${formattedCssPropertiesValues.join('')} }` //join convert from array to string. I'm using as separator the empty string to merge the items of the array sheet.insertRule(rule,0); //the first parameter is a string (its format is `selector{prop1:val1;prop2:val2}`) that represents your rule. The second parameter is the position in the stylesheet where you insert the rule console.log(rule) const children = parent.querySelectorAll("*"); //When you have an iterable object like an array or an HTMLCollection //you can iterate it whit a for of. `child` will be the current item for (let child of children) child.classList.add('yourClass'); } let parentNode = document.getElementById('my-parent'); styleChildNodes(parentNode,'opacity',0.5,'display','block');
 <link rel="stylesheet"> <div id="my-parent"> <span></span> <p></p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM