المحدد :focus
في jQuery
< jQuery
اذهب إلى التنقل
اذهب إلى البحث
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
المحدد :focus
الوصف
يحدد العنصر إذا كان مُركّزًا عليه حاليًا.
jQuery( ":focus" )
أُضيف مع الإصدار: 1.6.
كما هو الحال مع الأصناف الزائفة الأخرى (تلك التي تبدأ بنقطتين رأسيتين :
)، فمن المستحسن أن تسبق :focus
باسم وسم أو مُحدِّد آخر؛ خلاف ذلك، يستخدم المحدد العالمي (*
) كمحدد ضمني. وبعبارة أخرى ، فإن $( ":focus" )
يعادل $( "*:focus" )
.
بمعنى آخر، فإن $( ":focus" )
المجرد يعادل $( "*:focus" )
. إذا كنت تبحث عن العنصر المُركّز حاليًا ، فسيقوم $( document.activeElement )
باستعادته دون الحاجة إلى البحث في شجرة DOM بأكملها.
أمثلة
إضافة الصنف focused
إلى أي عنصر يملك التركيز حاليًا:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>focus demo</title>
<style>
.focused {
background: #abcdef;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="content">
<input tabIndex="1">
<input tabIndex="2">
<select tabIndex="3">
<option>select menu</option>
</select>
<div tabIndex="4">
a div
</div>
</div>
<script>
$( "#content" ).delegate( "*", "focus blur", function() {
var elem = $( this );
setTimeout(function() {
elem.toggleClass( "focused", elem.is( ":focus" ) );
}, 0 );
});
</script>
</body>
</html>