我有一个输入字段,单击它会打开一个 DIV 标记。如果我单击此 Div 上的任何位置,或者当我从 Div 中选择任何内容时,选择会转到输入字段并且 DIV 将被关闭。但是,在 DIV 打开并单击 DIV 之外的任何位置后,我希望 DIV 关闭。我尝试使用,event.target.id但它不会给我其他元素的 id。它只是让我空白。
JSP代码:
<div class="mRow">
<label for="SS">Special Subjects:</label>
<span class="numLbls">1. </span><input type="text" name="ade" value="<%=ade[0]%>" size="6" maxlength="6" onclick="showCodeLookup(this, 'divSpec')"/>
<span class="numLbls">2. </span><input type="text" name="ade" value="<%=ade[1]%>" size="6" maxlength="6" onclick="showCodeLookup(this, 'divSpec')"/>
<span class="numLbls">3. </span><input type="text" name="ade" value="<%=ade[2]%>" size="6" maxlength="6" onclick="showCodeLookup(this, 'divSpec')"/>
</div>
This is the DIV
<div id="divSpec" class="lookupTable" onClick="hideThis(this.id)">
<table>
<%
for (int i = 0; i < luSpec.size(); i++)
{
lu = (LookupTableBean) luSpec.get(i);
%>
<tr>
<td><%=lu.getCode()%>.</td>
<td><a href="javascript: setCode('divSpec', '<%=lu.getCode()%>')" ><%=lu.getDescr()%></a></td>
</tr>
<%
}%>
</table>
</div>
显示和隐藏 DIV 的 Javascript:
function showCodeLookup(el, divName)
{
//Hide any lookup tables that are displayed first
document.getElementById("divSpec").style.display="none";
var divCodes = document.getElementById(divName);
computeCoordinates();
codeEl = el;
divCodes.style.display="block";
divCodes.style.top='1000px';
divCodes.style.left='1000px';
}
function hideThis(id)
{
document.getElementById(id).style.display="none";
}
document.onclick = function(e){
alert(e.target.id); --this is always blank
};
但这总是给我一个空白。我使用的是纯 Javascript,没有 jQuery 库。