Rus*_* C. 4 html javascript css jquery
是否可以使用jQuery/Javascript实现直接更改标签文本的计数器?例如,如果我有2个标签:
<a>hello</a>
<a>bye</a>
Run Code Online (Sandbox Code Playgroud)
在运行jQuery/JS函数之后,会发生这种情况:
<a>[1]hello</a>
<a>[2]bye</a>
Run Code Online (Sandbox Code Playgroud)
我不能使用CSS计数器,因为我需要脚本来直接编辑HTML.
您可以使用 .html(function)
$("a").html(function(index, html) {
return "[" + (index + 1) + "]" + html;
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<a>hello</a>
<a>bye</a>
Run Code Online (Sandbox Code Playgroud)