我希望在页面上的两个输入都没有焦点时调用某个函数。如果用户只是从一个输入切换到另一个输入,我不希望调用该函数。有什么想法吗?
答案 0 :(得分:1)
试试这个(编辑过):
<input type="text" onfocus="doFocus(1)" onblur="doBlur(1)"/>
<input type="text" onfocus="doFocus(2)" onblur="doBlur(2)"/>
<script>
var fstate=[]; // focus state
fstate[1] = false;
fstate[2] = false;
function doit() { alert('lost focus'); }
function doBlur(i) {
fstate[i]=false;
// give some time to the other element to receive focus
// because the onblur event gets fired before the onfocus
setTimeout("if (!fstate[1] && !fstate[2]) doit();",50);
}
function doFocus(i) { fstate[i]=true; }
</script>
答案 1 :(得分:0)
试试这个:
<input type="text" Id="Text1" onfocus="InTextbox()" onblur="OutOfTextBox()"/>
<input type="text" id="Text2" onfocus="InTextbox()" onblur="OutOfTextBox()"/>
<script>
var inTextBox=false;
function InTextBox()
{
inTextBox=true;
}
function OutOfTextBox()
{
inTextBox=false;
}
function YourFunction()
{
if(inTextBox) return; //one of the textboxes has focus so return
.......
}
</script>
变量inTextBox在文本框聚焦时设置为true,在失去焦点时设置为false。