0

助けようと投稿してくれたすべての人に大いに感謝します。あなたたちは素晴らしいです!:)

@ jsfriend00ソリューションを使用しましたが、クライアントブラウザのキャッシュの問題に必要な機能を実行しました。

こんにちは私はjavascriptに関する質問があります:

テキストエリアがユーザーによって使用されているかどうかを検出する方法があるかどうか疑問に思っています。つまり、ユーザーは別のユーザーにメッセージを送信していますが、Enterキーストロークはまだありません。

ユーザーが別のユーザーにメッセージを送信しようとしていない場合にのみクライアントブラウザーのキャッシュを更新できれば、プライベートネットワークストリームのキャッシュの問題を解決できると考えていました。更新します。

たとえば、次のtextareaと言います。

 <html>
 <title></title>
 <head>
 <script>
 function autoRefresh(refreshPeriod) {
 var obj = document.getElementById("create"); 

     function refreshIfSafe() {
     if (document.activeElement !== obj) { 
        window.location.reload();
      } else {
        setTimeout(refreshIfSafe, refreshPeriod);
      }
   }
       setTimeout(refreshIfSafe, refreshPeriod);
 }

       autoRefresh(2 * 1000);

  </script>

  <?php echo smiley_js(); ?>

 </head>
 <body>


<?php echo form_open('controller/function') ?>
<p><label for="create">chat here:<span class="optional"></span></label>                                
<textarea name="create" id="create" onfocus="autoRefresh(this.id)" rows="3" cols="20"></textarea> 
<?php echo form_error('create'); ?>
</p>
<?php echo form_submit('submit','chat');?>
 <?php echo form_close();?>
 </body>
 </html>
4

3 に答える 3

2

フォーカスが現在テキストエリアにあるかどうかを知りたい場合は、document.activeElementがテキストエリアオブジェクトであるかどうかを確認することで確認できます。

var obj = document.getElementById("myTextArea");
if (document.activeElement === obj) {
    // focus is currently in my textarea
}

動作するデモを見ることができます:http://jsfiddle.net/jfriend00/NCSed/


テキストエリアがいつ変更されるかを知りたい場合は、テキストエリアに変更ハンドラーをインストールし、テキストエリアのテキストが変更されるたびにイベントハンドラーを呼び出すコードを次に示します。このコードは、textareaオブジェクトまたはinputオブジェクトのいずれかで機能します。他の方法とは異なり、これは、入力、マウス編集、切り取り、コピー、貼り付け、およびドラッグアンドドロップによって行われた変更をキャプチャします。次に、ユーザーがいつ入力しているか、またはテキストがいつ入力されたかを知ることができます。このコードの動作するテストエンジンは次のとおりです:http://jsfiddle.net/jfriend00/xxCkm/

そして、ここにコードがあります:

// add event cross browser
function addEvent(elem, event, fn) {
    if (elem.addEventListener) {
        elem.addEventListener(event, fn, false);
    } else {
        elem.attachEvent("on" + event, function() {
            // set the this pointer same as addEventListener when fn is called
            return(fn.call(elem, window.event));   
        });
    }
}

function onChange(elem, fn, data) {
    var priorValue = elem.value;

    function checkNotify(e, delay) {
        // log('checkNotify - ' + e.type);
        if (elem.value != priorValue) {
            priorValue = elem.value;
            fn.call(this, e, data);
        } else if (delay) {
            // the actual data change happens aftersome events
            // so we queue a check for after
            setTimeout(function() {checkNotify(e, false)}, 1);
        }
    }

    // Which events to monitor
    // the boolean value is whether we have to 
    // re-check after the event with a setTimeout()
    var events = [
       "keyup", false,
       "blur", false,
       "focus", false,
       "drop", true,
       "change", false,
       "input", false,
       "paste", true,
       "cut", true,
       "copy", true
    ];
    for (var i = 0; i < events.length; i+=2) {
        (function(i) {
            addEvent(elem, events[i], function(e) {
                checkNotify(e, events[i+1]);
            });
        })(i);
    }
}

次に、サンプルの使用法は次のようになります。

var obj = document.getElementById("test");    
onChange(obj, function(e) {
    // your code goes here for whatever you want to do 
    // when the change handler is called
    console.log("change - " + e.type);
});

</ h2>

あなたのコメントのコードを見て、私はこれを提案します:

<script>
function autoRefresh(refreshPeriod) {
    var obj = document.getElementById("create"); 

    function refreshIfSafe() {
        if (document.activeElement !== obj) { 
            window.location.reload();
        } else {
            setTimeout(refreshIfSafe, refreshPeriod);
        }
    }

    setTimeout(refreshIfSafe, refreshPeriod);
}

autoRefresh(2 * 1000);
</script>

于 2012-09-14T03:29:59.860 に答える
0
var textarea = document.getElementById("create") ;
textarea.onfocus = function() {
    // Do something while the textarea being used by a user
};

textarea.onblur = function() {
    // Do something while the textarea not being used by a user
};
于 2012-09-14T03:08:55.443 に答える
-1

これは実際、jQueryを使用すると簡単になるという非常にまれなケースの1つです。

if($("#create").is(":focus")) {
    // element is in use
}

代替案は次のようになります。

document.body.onfocus = function(e) {
    e = e || window.event;
    window.elementWithFocus = e.target || e.srcElement;
}
...
if( document.getElementById('create') == window.elementWithFocus) {
    // element is in use
}
于 2012-09-14T03:07:34.730 に答える