<ul> <li>one</li> <li>two</li> <li>three</li> <li>four</li> </ul>
<script> // $(this).index() 測試代碼一 $("li").on("click",function(){ var index = $(this).index(); console.log(index); // 依次點擊li元素輸出: 0,1,2,3 }) // $(obj).index(this) 測試代碼二 $("li").on("click",function(){ var index = $("li").index(this); console.log(index); // 依次點擊li元素輸出: 0,1,2,3
}) </script>
執行兩段腳本並依次點擊li元素,都是輸出: 0/1/2/3,在這個場景下,作用一樣。
var reg = /^[0-9]{5}$/i; var hasfour = true; $(".nocode").html(""); $(".vcodetip").show(); $("input[name='vcode']").each(function (i, item) { if (!reg.test($(this).val())) { if ($(this).val().length <= 4) { if (hasfour == true){//定位焦點到第一個未填完整 $(this).focus(); hasfour = false; } $("div[class='vcodetip']").eq($(this).index() - 1).css("visibility", "visible");//顯示提示 } //return false;//確定是否所有未輸入完成的都提示 } });
<div class="Step1div"> <span style="width:56px;">防偽碼: </span> <input id="code1" type="text" name='vcode' class="inputcode" style="font-size: 14px;" placeholder="第一組" maxlength="5"> <input id="code2" type="text" name='vcode' class="inputcode" style="font-size: 14px;" placeholder="第二組" maxlength="5"> <input id="code3" type="text" name='vcode' class="inputcode" style="font-size: 14px;" placeholder="第三組" maxlength="5"> <input id="code4" type="text" name='vcode' class="inputcode" style="font-size: 14px;" placeholder="第四組" maxlength="5"> <a href="javascript:void(0);" class="Nextbtn" style="color:white" onclick="getTelInfo()">下一步</a> </div> <div class="Step1div tip"> <span class="space">   :</span> <div class="nocode"></div> <div class="vcodetip">請輸入5位驗證碼</div> <div class="vcodetip">請輸入5位驗證碼</div> <div class="vcodetip">請輸入5位驗證碼</div> <div class="vcodetip">請輸入5位驗證碼</div> </div>