gpt4 book ai didi

javascript - 获取特定标签的索引

转载 作者:行者123 更新时间:2023-11-29 19:16:48 24 4
gpt4 key购买 nike

如何只获取点击段落的索引,而不计算文本区域的索引?

html代码:

<div class="story">
<p>index is 0, that's ok</p>
<textarea></textarea>
<p>index is 2, should be 1</p>
<textarea></textarea>
<p>index is 4, should be 2</p>
</div>

js:

$(".story > p").click(function() {
var a = $(this).index();
alert (a);
});

最佳答案

$.index(selector) :代表要在其中查找元素的 jQuery 集合的选择器。

$(".story > p").click(function() {
var a = $(this).index("p");
alert (a);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="story">
<p>index is 0, that's ok</p>
<textarea></textarea>
<p>index is 2, should be 1</p>
<textarea></textarea>
<p>index is 4, should be 2</p>
</div>

关于javascript - 获取特定标签的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34722787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com