Javascript div计数问题 [英] Javascript div count problem

查看:76
本文介绍了Javascript div计数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一些具有相同ID的div,例如

 <   div     id   =  skillDiv >  
< ; span id < span class =code-keyword> = otherChk > test < / span >
< / div >
< div id = skillDiv >
< span id = otherChk > good < / span >
< / div >







现在我想计算id为'skillDiv'的div的总数,我使用此代码

  var  elements = document.all('  skillDiv')。length; 





这里只有一个div存在然后它显示element = undefined如果div存在多个,那么它显示正确的number.how来解决这个问题? Plz帮助我。



我尝试过:



var元素= document.all('skillDiv')。length;

解决方案

这里的id必须是唯一的。它用于该元素的身份。而不是使用类。


jquery中的




 


.classname) .length





会给你计算。


Id 应该是唯一的。如果你想为某些元素提供一些常见的东西,可以使用属性并将相同的类分配给你想要的所有元素。

< pre lang =HTML> < div class = skillDiv < span class =code-keyword>>
< span id = otherChk > test < / span >
< / div >
< div = skillDiv >
< span id = otherChk > 良好< / span >
< / div >



现在,您可以按照提供的公共类选择元素。


i use some div with same id like

<div id="skillDiv">
<span id="otherChk">test</span>
</div>
<div id="skillDiv">
<span id="otherChk">good</span>
</div>




Now i want to count total number of div which id is 'skillDiv' and i use this code

var elements = document.all('skillDiv').length;



here when only one div is exist then it show element = undefined and if div is exist more than one then it show the right number.how to solve this problem? Plz help me.

What I have tried:

var elements = document.all('skillDiv').length;

解决方案

Here id must be unique. It is used for identity of that element.use class instead.

in jquery


(".classname").length



will give you the count.


Id should be unique. If you want to provide some common thing to some elements, use the class property and assign the same class to all the elements you want.

<div class="skillDiv">
<span id="otherChk">test</span>
</div>
<div class="skillDiv">
<span id="otherChk">good</span>
</div>


Now you can select elements by the common class you have provided.


这篇关于Javascript div计数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆