0
我有一個問題,當我分離當前的焦點元素,並再次插入使用Tab鍵移動字段。請在jsfiddle.找到我的代碼。另見下文:問題與焦點()後detach和insertBefore
JS代碼:
$(document).ready(function() {
$('.formElem').focusin(function() {
// Remove default text on focus in
if ($(this).val() == $(this).attr('title')) {
$(this).val('').removeClass('defaultText');
if (($(this).attr('id') == 'reg_pwd') || ($(this).attr('id') == 'reg_conf_pwd')) {
id = '#' + $(this).attr('id');
marker = $('<span>123</span>').insertBefore($(this));
$(this).detach().attr('type', 'password').insertAfter(marker);
marker.remove();
}
if ($(this).get(0) != $(':focus').get(0)) {
$(this).focus();
}
}
}).focusout(function() {
// Remove default text on focus out
if ($(this).val() === '') {
$(this).val($(this).attr('title')).addClass('defaultText');
if (($(this).attr('id') == 'reg_pwd') || ($(this).attr('id') == 'reg_conf_pwd')) {
marker = $('<span>123</span>').insertBefore($(this));
$(this).detach().attr('type', 'text').insertAfter(marker);
marker.remove();
}
}
});
});
代碼從文本改變字段的類型密碼和來回。當您單擊或使用Tab鍵到達密碼字段時,它會在重新添加後失去焦點。謝謝如果有人可以找出原因。
我只是在JSFiddle中迷惑同樣的事情。 HTML5佔位符屬性是否不解決這種類型代碼的需求? –
目前還不清楚他想要用123跨度做什麼。此外,一些瀏覽器(即MSIE)在創建後不會讓您更改輸入的類型。 – Alnitak
跨度充當重新插入元素的標記。我沒有研究過MSIE,只是探索這種技術。 – Pradeep