Applying CSS on class with variable name

Chankey Pathak

I'm generating class name from server side like:

<p class="level_1">List item 1</p>
<p class="level_2">List item 2</p>
<p class="level_3">List item 3</p>
<p class="level_1">List item 1</p>
<p class="level_2">List item 2</p>
<p class="level_1">List item 1</p>

and I expect the output with indentation such that it looks like:

List item 1
   List item 2
      List item 3
List item 1
   List item 2
List item 1

The syntax for class name is like level_$i where $i is variable and it can go to any level (starting from 1 to n). So how can I apply CSS for indentation in this situation?

henser

You can't do for loops in pure CSS. Consider using LESS for that purpose. Here's a tutorial on LESS Loops

You can achieve this using jQuery ... see the jsFiddle example I prepared for you.

var i= 1,
    val= 0;

$('[class^=level_]').each(function(){

$('.level_' + i).css('padding-left',val+'px');
    i++;
    val += 20;
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SCSS variable class name

分類Dev

SCSS variable class name

分類Dev

SCSS variable class name

分類Dev

Python class variable name vs __name__

分類Dev

Can class name and variable name be same in java?

分類Dev

How would I create a class with a variable name in the class name?

分類Dev

JS class not applying on datapicker

分類Dev

Get The Class Name From an Object Variable

分類Dev

Using Part of a Class Name as a Variable for .addClass

分類Dev

Applying CSS to a MediaWiki site

分類Dev

Applying CSS to template field

分類Dev

CSS selector for multiple different class name combinations

分類Dev

Class combination not applying new style

分類Dev

Tailwind css background gradient not applying

分類Dev

Applying CSS on JavaFx TreeTableView fails

分類Dev

Passing a variable value, rather than its name, as a class

分類Dev

How to disable properties of external css class and use internal css class with same name but different property in html

分類Dev

Using only CSS is it possible to set a property based on class name?

分類Dev

How to append css Class name to particular image src url

分類Dev

Undefined reference to class_name<template_argument>function_name(variable)?

分類Dev

class_name <template_argument> function_name(variable)への未定義の参照?

分類Dev

How to check if a member name (variable or function) exists in a class, with or without specifying type?

分類Dev

Why does assigning to a class attribute with the same name as a local variable raise a NameError?

分類Dev

Why javascript static variable not able to access ( using class name ) without creating minimum one instance?

分類Dev

When the name of a css class in Chrome DevTools is only an asterisk, what does the asterisk mean?

分類Dev

Variable string inside variable name

分類Dev

Why is setTimeout needed when applying a class for my transition to take effect?

分類Dev

Applying style through hover using JavaScript doesn't work with class

分類Dev

Applying jQuery function to one div class with randomly given numbers?

Related 関連記事

  1. 1

    SCSS variable class name

  2. 2

    SCSS variable class name

  3. 3

    SCSS variable class name

  4. 4

    Python class variable name vs __name__

  5. 5

    Can class name and variable name be same in java?

  6. 6

    How would I create a class with a variable name in the class name?

  7. 7

    JS class not applying on datapicker

  8. 8

    Get The Class Name From an Object Variable

  9. 9

    Using Part of a Class Name as a Variable for .addClass

  10. 10

    Applying CSS to a MediaWiki site

  11. 11

    Applying CSS to template field

  12. 12

    CSS selector for multiple different class name combinations

  13. 13

    Class combination not applying new style

  14. 14

    Tailwind css background gradient not applying

  15. 15

    Applying CSS on JavaFx TreeTableView fails

  16. 16

    Passing a variable value, rather than its name, as a class

  17. 17

    How to disable properties of external css class and use internal css class with same name but different property in html

  18. 18

    Using only CSS is it possible to set a property based on class name?

  19. 19

    How to append css Class name to particular image src url

  20. 20

    Undefined reference to class_name<template_argument>function_name(variable)?

  21. 21

    class_name <template_argument> function_name(variable)への未定義の参照?

  22. 22

    How to check if a member name (variable or function) exists in a class, with or without specifying type?

  23. 23

    Why does assigning to a class attribute with the same name as a local variable raise a NameError?

  24. 24

    Why javascript static variable not able to access ( using class name ) without creating minimum one instance?

  25. 25

    When the name of a css class in Chrome DevTools is only an asterisk, what does the asterisk mean?

  26. 26

    Variable string inside variable name

  27. 27

    Why is setTimeout needed when applying a class for my transition to take effect?

  28. 28

    Applying style through hover using JavaScript doesn't work with class

  29. 29

    Applying jQuery function to one div class with randomly given numbers?

ホットタグ

アーカイブ