我从 w3schools 获取了此代码示例https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_html_callback
我想增加索引号,我该如何做到这一点而不在 .text 和 .html 代码块上显示零
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text(function(i, origText){
return "Old text: " + origText + " New text: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i, origText){
return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + i + ")";
});
});
});
</script>
</head>
<body>
<p id="test1">This is a <b>bold</b> paragraph.</p>
<p id="test2">This is another <b>bold</b> paragraph.</p>
<button id="btn1">Show Old/New Text</button>
<button id="btn2">Show Old/New HTML</button>
</body>
</html>
return "Old text: " + origText + " New text: Hello world! (index: " + (i + 1) + ")";
return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + (i + 1) + ")";
在字符串中使用
(i + 1)
。