-1

.index() で非常に奇妙な問題が発生しています。私はローカルで何かを構築しており、すべてが正常に機能しています。コードを WAMP/MAMP ローカル ホスティングに配置するとすぐに、.index() カウントが正常に機能しなくなります。

var prodCont = $(".disaCats");

prodCont.each(function(){
//Checking too see how Many products the category has
var tempIndex = $(this).children(".cardContainer").index();

//If the there are more than 6 products show the dropdownArrow
if(tempIndex > 5){
      $(this).siblings(".disaHeading").children(".showMoreArrow").show();
});//end index calculation

アラートを使用して tempIndex を返したところ、各項目に対して 0 が返されました。

クラスセレクターなしで .index(this) と .children() だけを使用してみましたが、同じことを行います。これは WAMP/MAMP の問題だと思い始めています。

どんな助けでも大歓迎です。

編集: このスクリプトは、WAMP/MAMP の localhost/ を介して正常に実行されますが、IP 521.xxx.xxx/ を使用して共有しようとするとすぐに、インデックス カウントが適切に機能しなくなります。

4

3 に答える 3

1

Look at what index() does. The documentation says:

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

You do not want index! You want to know the number of children elements. To get that, you want to use .length

var tempIndex = $(this).children(".cardContainer").length;
于 2013-09-19T17:42:50.127 に答える
1

コレクション内の要素の数を取得するには、次のようにindex()置き換えます。length

//Checking too see how Many products the category has
var tempIndex = $(this).children(".cardContainer").length;
于 2013-09-19T17:42:59.563 に答える