簡體   English   中英

為什么focus()方法不起作用?

[英]Why focus() method is not working?

focus()的register.phtml頁面上使用了相同的代碼,但personal.phtml上不使用該代碼。

<script>
    $(document).ready(function(){
        $("#rut").blur(function(){
            document.getElementById('error12').innerHTML ="";
            var Rut = $("#rut").val();
            var RemoveDotInRut = Rut.split('.').join("");
            var RemoveHypenInRut = RemoveDotInRut.split('-').join("");
            var freshString = RemoveHypenInRut.length;
            if(freshString<8 || freshString>9){
                document.getElementById('error12').innerHTML = "<?php echo $this->__('Please enter a valid Rut number.') ?>";
                $("#rut").val('');
                $("#rut").focus();
            }

        });
    });
    </script>

一切都在上述腳本中完成,但是focus()無法正常工作。 為什么?

模糊功能結束后,焦點將丟失,因此在模糊功能內設置焦點無效

更換

    $("#rut").focus();

setTimeout(function() {
    $("#rut").focus();
}, 0);

它將在下一個事件循環中“重新聚焦”元素

使用: 演示

將您的.focus()移到那里:

$(this).delay(0).queue(function() {
   $(this).focus().val("").dequeue();;
});

另外,我已經優化了您的代碼。 $("#rut")已替換為$(this)

使用以下代碼:

$("#focusedDiv").focus(function () {
    ....
});

點擊這里

從這里開始嘗試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM