仅关注输入类型文本或电话,而不是收音机或复选框
on focus only for input type text or tel, not radio or checkboxes
我正在使用此代码自动滚动到选定的输入类型:
$( document ).on( "focus", "input", function() {
if(document.activeElement.tagName=="INPUT"){
window.setTimeout(function(){
document.activeElement.scrollIntoView();
},0);
}
return false;
});
问题是,我想排除单选按钮和复选框..并且只将其用于输入类型 tel
或 text
。我该如何实现?
您可以根据类型仅 select 相关输入,而不是 select 所有输入标签:
$( document ).on( "focus", "input[type='tel'],input[type='text']", function() {
console.log('relevant element focused');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" />
另一种选择是检查聚焦元素的 type
,并在 selected 类型的情况下中断函数的 运行:
$( document ).on( "focus", "input", function() {
if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
return;
}
console.log('relevant element focused');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" />
我正在使用此代码自动滚动到选定的输入类型:
$( document ).on( "focus", "input", function() {
if(document.activeElement.tagName=="INPUT"){
window.setTimeout(function(){
document.activeElement.scrollIntoView();
},0);
}
return false;
});
问题是,我想排除单选按钮和复选框..并且只将其用于输入类型 tel
或 text
。我该如何实现?
您可以根据类型仅 select 相关输入,而不是 select 所有输入标签:
$( document ).on( "focus", "input[type='tel'],input[type='text']", function() {
console.log('relevant element focused');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" />
另一种选择是检查聚焦元素的 type
,并在 selected 类型的情况下中断函数的 运行:
$( document ).on( "focus", "input", function() {
if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
return;
}
console.log('relevant element focused');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" value="1" />
<input type="text" value="this is some text" />
<input type="tel" value="123456789" />
<input type="checkbox" value="123456789" />