each
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>溫故而知"心"</title>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('li').each(function (index, ele) {
$(ele).text(index + 1).addClass('demo' + index)
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>溫故而知"心"</title>
</head>
<body>
<p>
<i></i>
<span></span>
<i></i>
<span></span>
</p>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('p').children().each(function (index, ele) {
if (ele.nodeName == "I") {
$(ele).text(`i-${index}`)
} else {
$(ele).text(`span-${index}`)
}
})
</script>
</html>
index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>溫故而知"心"</title>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('ul').on('click', 'li', function (e) {
console.log($(e.target).index())
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>溫故而知"心"</title>
</head>
<body>
<p>
<i>1</i>
<span>2</span>
<i>3</i>
<span>4</span>
</p>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('p').children().on('click', function (e) {
console.log($('p span').index($(e.target)))
})
</script>
</html>