ฉันจะยกเลิกการโฟกัสข้อความหรืออินพุตได้อย่างไร ฉันหา$('#my-textarea').unfocus();
วิธีไม่เจอ?
ฉันจะยกเลิกการโฟกัสข้อความหรืออินพุตได้อย่างไร ฉันหา$('#my-textarea').unfocus();
วิธีไม่เจอ?
คำตอบ:
$('#textarea').blur()
เอกสารประกอบที่: http://api.jquery.com/blur/
$('#textarea').bind('blur', function() ...)
ยัง
จากคำถามของคุณฉันเชื่อว่าคำตอบคือวิธีการทำให้เกิดภาพเบลอไม่ใช่เพียงแค่ตั้งค่าเหตุการณ์ (หรือแม้กระทั่ง):
$('#textArea').trigger('blur');
คิดว่าคุณกำลังมองหา .focusout()
focusout
จะเริ่มทำงานหลังจากอินพุตเริ่มแล้วที่จะสูญเสียโฟกัส ถามต้องการที่จะใส่องค์ประกอบในสถานะที่เรียกกลับ SOA จัดการจะทำเขาไม่ดีdeveloper.mozilla.org/en-US/docs/Web/Reference/Events/focusout api.jquery.com/focusout
สิ่งนี้ใช้ได้กับฉัน:
// Document click blurer
$(document).on('mousedown', '*:not(input,textarea)', function() {
try {
var $a = $(document.activeElement).prop("disabled", true);
setTimeout(function() {
$a.prop("disabled", false);
});
} catch (ex) {}
});
ฉันชอบวิธีการต่อไปนี้เพราะมันใช้ได้กับทุกสถานการณ์:
$(':focus').blur();
ดังนั้นคุณสามารถทำได้
$('#textarea').attr('enable',false)
ลองและให้ข้อเสนอแนะ
.focusout()
ที่แตกต่างจากblur()
api.jquery.com/focusoutเล็กน้อยโดยอ้างถึงเอกสารThis is distinct from the blur event in that it supports detecting the loss of focus on descendant elements (in other words, it supports event bubbling)