jQueryの:どのように入力ボックスクラスセレクタベースの機能
$("#x1").focus();
$('#x1').get(0).focus();
$('.inputTextBox').focus(function() {
//This is where I need help... how to get the
//element of the element that is getting focus?
var myElement = document.getElementById('x1');
myElement.style.backgroundColor = "#A7C942";
myElement.style.color = "black";
myElement.style.fontSize = "2em";
});
$('inputTextBox').blur(function() {
//This is where I need help... how to get the
//element of the element that is losing focus?
var myElement = document.getElementById('x1');
myElement.style.backgroundColor = "gray";
myElement.style.color = "black";
myElement.style.fontSize = "2em";
});
.inputTextBox {
background: gray;
background-color: gray;
color: black;
font-size: 2em
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>
<div data-role="content">
<fieldset>
<label>x1</label>
<input id="x1" , autofocus="autofocus" , class="inputTextBox" />
</fieldset>
<fieldset>
<label>x2</label>
<input id="x2" , class="inputTextBox" />
</fieldset>
<fieldset>
<label>x3</label>
<input id="x3" , class="inputTextBox" />
</fieldset>
<input type="submit" value="Log in" style="width:100%" />
</div>
</body>
</html>
で要素を取得すると、私が発射された要素を取得する方法を見つけ出す必要があるので、Ids
が自動的に生成されますfocus
およびblur
イベント。
何とかこれらを渡すことはできますか?またはDOMから取得できますか?
メモ:入力タグ内でOnfocusとonblurを使用しようとしましたが、これはchromeでのみ有効です。私はこれをIEのために働かせることはできません。
2番目の質問:コントロール要素の上にタブを置く方法はありますか?私はボタンを通過するとき、私は最初のテキストボックスにループしたいと思いますが、代わりにヘッダの最初の要素をヒットします。
まあ、私はあなたが使用しようとしているjQueryライブラリが参照されていませんので、これはどこでも動作します驚いています...また、 '$(「inputTextBox」)' '$でなければなりません( '.inputTextBox') '。がありません。 – Pogrindis
フォーカスを失っている要素の要素を取得する方法は?セレクタとして '$(this)'を使用してください。 –
'var curElement = document.activeElement;'は現在フォーカスされている要素を返します。 https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElementを参照してください –