js如何设置一个自增的标识id

JavaScript022

js如何设置一个自增的标识id,第1张

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<script type="text/javascript">

function next_id(){

var current_id = 0

return function (){

return ++current_id

}

}

var g = next_id()

for( let i = 0i <10i++ ){

console.log(g())

}

</script>

<body>

</body>

</html>

var ele = document.getElementById('id_name')

ele.id = 'id_name'

ele.setAttribute('name', 'name_value')