我需要清空并专注于按键输入的 textarea。我想我写了正确的代码,但有时 textarea 变空但没有焦点发生,有时发生焦点但没有运气清空这个对象。我使用了许多不同的代码,但使用没运气。
这是我的js:
<script>
$(document).ready(function() {
$("#data").focus(function() {
$(this).empty();
$(this).focus();
});
})
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
$this = $(this);
if($this.val().length == 1)
{
var x = new RegExp("[\x00-\x80]+"); // is ascii
var isAscii = x.test($this.val());
if(isAscii)
{
$("#data").css({"direction": "ltr", "text-align": "left"});
} else {
$("#data").css({"direction": "rtl", "text-align": "right"});
}
}
if (e.shiftKey && e.keyCode == 13) {
$(this).val($(this).val() + "\n");
return;
}
if(e.which == 13) {
if($this.val().length == 0){
alert('your value is empty')
}
var now = time();
if(now+0.5 > counter){
counter = now;
$(this).blur();
$('#datasend').focus().click();
}else{
}
}
});
});
这是我的html部分:
<div class="wrap">
<div class="rooms">
<div class="clear"></div>
<div class="roomsicon">
chatrooms
</div>
<ul id="rooms">
</ul>
</div>
<div class="chat_box_container">
<div class="chat_box" id="mydiv">
<div id="conversation"></div>
</div>
<div class="input_field">
<textarea id="data" placeholder="type your text ... " ></textarea>
<input type="button" id="datasend" value="send" />
</div>
</div>
<div class="users">
<div class="usersicon">online users </div>
<ul id="users">
</ul>
</div>
</div>
<!-- clear float -->
<div class="clear"></div>