js dynamically adds example code for a list of circled numbers

js dynamically adds example code for a list of circled numbers

1. Add the ul tag in the body first

<!-- Unordered list -->
<ul id="list">
	
</ul>

2. Get the tag with id equal to list through js, define an empty string to connect the added tags, and display them

The js code shown in the figure shows the circle numbers with different colors for the first three and the same colors for the remaining circles.

function autoAddList(){
  var oUl = document.getElementById('list');
  // var arr = ['Hunan', 'Guangxi', 'Xinjiang', 'Shanghai']
  var str = "";
  for (let i = 1; i < 13; i++) {
    if (i == 1) {
      str += '<li><a style="color: white"><span id="listOne" style="background-color: rgb(187, 7, 7)">' + i + '</span></a></li>' + '<br>';
    }else if (i == 2) {
      str += '<li><span style="background-color: #ffad33">' + i + '</span></li>' + '<br>';
    }else if (i == 3) {
      str += '<li><span style="background-color: green">' + i + '</span></li>' + '<br>';
    }else{
      str += '<li><span style="background-color: #339cff">' + i + '</span></li>' + '<br>';
    }    
  }
  oUl.innerHTML = str;
}

3.css style modification

/*Set the list style*/
ul{
  list-style-type: none;
}

list-style-type: none means no logo, and the attributes can also be hollow circles, solid squares, numbers, etc.

To arrange the serial numbers neatly, you need to set the span style

/*Set as inline block element*/
li span{
	display:inline-block;
}

The effect is shown in the figure below

insert image description here

This is the end of this article about how to dynamically add a circled number list with js. For more relevant content about how to dynamically add a circled number list with js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript array, JSON object to achieve dynamic addition, modification, deletion function example
  • JS clicks to dynamically add tags and delete specified tags
  • An example of how to add HTML tags dynamically using JS
  • JS implementation example of adding event functions to dynamically added elements [based on event delegation]
  • How to add serial numbers to AngularJS tables
  • JavaScript automatically adds a serial number in front of the table

<<:  How to use the Linux more command in Linux common commands

>>:  Detailed explanation of MySql 5.7.17 free installation configuration tutorial

Recommend

Detailed explanation of how to use the Vue license plate search component

A simple license plate input component (vue) for ...

Sample code for deploying ELK using Docker-compose

environment Host IP 192.168.0.9 Docker version 19...

Implementation of Nginx forwarding matching rules

1. Regular expression matching ~ for case-sensiti...

Detailed explanation of DOM style setting in four react components

1. Inline styles To add inline styles to the virt...

Why should the number of rows in a single MySQL table not exceed 5 million?

Today, let’s discuss an interesting topic: How mu...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...

A brief discussion on macrotasks and microtasks in js

Table of contents 1. About JavaScript 2. JavaScri...

Detailed explanation of MySQL slow log query

Slow log query function The main function of slow...

The pitfall record of the rubber rebound effect of iOS WeChat H5 page

Business requirements One of the projects I have ...

Detailed explanation of how to use Vue self-nested tree components

This article shares with you how to use the Vue s...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later wi...