2012-03-11 54 views
0

如何获得以前的焦点元素?如何获得以前的重点元素?

说明:

当用户点击phonepad键,它不应该主动或集中。 以便:focus或activeElement返回上一个焦点元素(textarea或input-text)。

<script> 
    $(function(){ 
     $(".phonepad").prop("disabled", true); 
     $('.phonepad').mousedown(function() { preventDefault();}); 
     $('.phonepad input').mousedown(function() { preventDefault();}); 
     $('.phonepad input').click(function(){ 
      $(document.activeElement).sendkeys(
         this.name || this.value); 
        ); 
     }); 
    }); 
</script> 
<div class="phonepad" > 
<input type="button" value="7" ><br> 
<input type="button" value="4" ><br> 
</div><br/> 
<input type="text" class="output" /><br/> 
<textarea class="output"></textarea><br/> 

回答

0

将mousedown事件处理程序附加到那些调用preventDefault()的按钮。

+0

也可以使用的focusIn()事件的内容()输入元素上的方法,你要处理的重点 – 2012-03-11 01:26:04

+0

我认为这仍可能拨打'的preventDefault()'调用前的'blur'事件。 – 2012-03-11 01:26:30

+0

要求是:当用户点击手机键时:1)然后焦点textarea或输入文本不应该失去它的焦点。 2)phonepad不应该得到重点。 3)用户点击手机按键后,focus或者getactiveelement应该返回先前的focus元素。 – 7Keypad 2012-03-11 01:44:14

1

您可以使用隐藏元素跟踪当前焦点的元素。这样,当另一个元素获得焦点时,它将保存前一个元素ID。当单击焦点启用元素时,其ID将存储在隐藏元素中。当单击焦点被禁用的元素时,焦点会放回到之前的焦点元素上,而'active_focus_enabled_id'隐藏元素不会被更新。您需要使用类来确定哪些元素能够/无法获得焦点,并确保所有启用焦点的元素都有一个ID。

<input type='hidden'id='active_focus_enabled_id' value=''id_with_initial_focus'> 

<input type='button' class='focus_enabled' id='id_with_initial_focus' value='Whatever #1'> 
<input type='button' class='focus_enabled' id='some_other_id' value='Whatever #2'> 
... 

<input type='button' class='focus_disabled' value='Phone Input #1'> 
<input type='button' class='focus_disabled' value='Phone Input #2'> 
... 

<script> 
$('.focus_disabled').click(function(event) { 
    var id_previous = $('active_focus_enabled_id').val(); 
    $(''#' + id_previous).focus(); 
}); 

$('.focus_enabled').click(function(event) { 
    var id_new = $(event.target).attr('id'); 
    $('active_focus_enabled_id').val(id_new); 
}); 
</script>