<asp:TextBox ID="txtBorderColor" runat="server" onfocus="showColorSelector(this);"></asp:TextBox>
function showColorSelector(colorTextBox) {
currentColorTextBox = colorTextBox;
show(colorSelector);
setAbsolutePosRight(colorSelector, colorTextBox);
document.body.attachEvent("onclick", decideOnOff);
}
function decideOnOff() {
var event = arguments[0] || window.event;
if (event.clientX + getHScroolPos() < colorSelector.offsetLeft
|| event.clientX + getHScroolPos() > colorSelector.offsetLeft + colorSelector.scrollWidth
|| event.clientY + getVScroolPos() < colorSelector.offsetTop
|| event.clientY + getVScroolPos() > colorSelector.offsetTop + colorSelector.scrollHeight) {
hide(colorSelector);
document.body.detachEvent("onclick", decideOnOff);
}
}
为什么点击TextBox后,弹出的Div一闪而过,就好像TextBox的onfocus事件之后,紧接着触发了body.onclick事件,而我想要的结果是当TextBox获取焦点后,显示一个Div,我点除TextBox和弹出的Div之外的区域时,Div消失,怎么搞啊?
你可以在body的onclick中通过判断srcElement或target属性,得知是在哪一个元素上单击造成的些事件的触发,如果发现是在textbox上单击的,那么就不需要再执行decideOnOff中的逻辑