programing

입력에 초점이 맞추어져 있는지 확인하는 Javascript/jQuery

lastcode 2023. 10. 9. 23:18
반응형

입력에 초점이 맞추어져 있는지 확인하는 Javascript/jQuery

다음 시간을 감지하려면 어떻게 해야 합니까?.click텍스트 영역이 이미 포커스된 경우 이벤트 트리거?

나는 다음과 같은 질문을 받고 있습니다.

$(".status").on("click","textarea",function(){
        if ($(this) == "focused") {
            // fire this step
        }else{
            $(this).focus();
            // fire this step
    }

순수 자바스크립트 사용:

this === document.activeElement // where 'this' is a dom object

아니면 jquery's로:focus의사선택기

$(this).is(':focus');

JQuery를 사용할 수 있는 경우 JQuery:Focus Selector를 사용하면 필요한 작업을 수행할 수 있습니다.

$(this).is(':focus');

jQuery의 .is( ":focus" )를 사용합니다.

$(".status").on("click","textarea",function(){
        if ($(this).is( ":focus" )) {
            // fire this step
        }else{
                    $(this).focus();
            // fire this step
    }

해보셨습니까?

$(this).is(':focus');

jQuery를 사용하여 입력에 포커스가 있는지 여부를 테스트합니다. 몇 가지 예제가 더 있습니다.

언급URL : https://stackoverflow.com/questions/17614844/javascript-jquery-detect-if-input-is-focused

반응형