disappears when an input within the form gains focus and the mouse cursor hovers in and out.
To address this, jQuery can be used to check if any input within the form has focus. However, as jQuery lacks a :focus selector, alternative methods must be employed.
jQuery 1.6+
With jQuery 1.6 and above, a :focus selector is available. The following code can be used to check for focus:
$("..").is(":focus")
登入後複製
jQuery 1.5 and Below
For earlier jQuery versions, the following code can be used to define a new selector and check for focus:
jQuery.expr[':'].focus = function(elem) {
return elem === document.activeElement && (elem.type || elem.href);
};
登入後複製
Alternatively, you can check which element has focus using:
$(document.activeElement)
登入後複製
For All jQuery Versions
To ensure compatibility with different jQuery versions, you can add the :focus selector as follows:
(function ($) {
var filters = $.expr[":"];
if (!filters.focus) {
filters.focus = function(elem) {
return elem === document.activeElement && (elem.type || elem.href);
};
}
})(jQuery);
登入後複製
By testing for focus, the border can be prevented from disappearing when an input gains focus and the cursor hovers in and out.
以上是如何使用 jQuery 檢查輸入是否有焦點?的詳細內容。更多資訊請關注PHP中文網其他相關文章!