如何找到一个div中有多少个选择标签?



在html中,我有一个div标签。在此div标签之间还有许多其他标签,如spanselectp标签,如下所示:

<div id="show">
<span>Span text</span>
<span>Span text</span>
<span>Span text</span>
<select id="select1">
<option>1</option>
</select>
<select id="select2">
<option>1</option>
</select>
</div>

现在我们如何发现有多少select tagsdivid使用jQueryid="show".谁能帮我找出这个。

您可以使用length

$('#show select').length

console.log($('#show select').length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="show">
<span>Span text</span>
<span>Span text</span>
<span>Span text</span>
<select id="select1">
<option>1</option>
</select>
<select id="select2">
<option>1</option>
</select>
</div>

选择每个select标记,这些标记是 idshowdiv子标记。

另外,请参阅 jquery.length

console.log($("div#show > select").length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="show">
<span>Span text</span>
<span>Span text</span>
<span>Span text</span>
<select id="select1">
<option>1</option>
</select>
<select id="select2">
<option>1</option>
</select>
</div>

这可能对您有所帮助。

$('#show select').length

最新更新