JSP:将 DIV 定位在特定坐标处不起作用

我正在尝试在输入框的坐标上放置一个Div。单击输入框,将启用 DIV 并加载值,并且 DIV 上的任何选择都必须将值设置为输入框。一切正常,除了输入框不是位于坐标上,而是位于页面顶部。


<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>

divSpec 是:


<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>

用于查找的 css 是:


.lookupTable

{

  display:none;

  padding:5px;

  z-index:10;

  font-size: 10px; 

  position: absolute;

  border: 2px solid #933; 

  background-color:white; 

  width: 220px; 

  height:180px; 

  overflow:auto;

}

Javascript


// Detect if the browser is IE or not.

// If it is not IE, we assume that the browser is NS.

var IE = document.all?true:false


var codeEl;


// If NS -- that is, !IE -- then set up for mouse capture

if (!IE) document.captureEvents(Event.MOUSEMOVE)


// Set-up to use getMouseXY function onMouseMove

document.onmousemove = getMouseXY;


}



任何建议请。我已经添加了警报 showCodeLookup(),它给了我正确的坐标,比如 1036 表示 Y,536 表示 X。


富国沪深
浏览 129回答 1
1回答

POPMUISE

我已经在Chrome上测试了您的代码,发现在您的js代码中并且是空的或未定义的。因此,当您将这些值分配给 div 时,它并未将其定位到所需的坐标。相反,我已经在您的js代码中进行了某些更改,如下所示:document.Show.sMouseY.valuedocument.Show.sMouseX.valuedivSpecvar IE = document.all ? true : falsevar codeEl;// If NS -- that is, !IE -- then set up for mouse captureif (!IE) document.captureEvents(Event.MOUSEMOVE)// Set-up to use getMouseXY function onMouseMovedocument.onmousemove = getMouseXY;// Temporary variables to hold mouse x-y pos.svar tempX = 0;//declare&nbsp;var tempY = 0;//declarevar mouseX = 0;var mouseY = 0;//var frm = document.dashboardSearchForm;function getMouseXY(e) {&nbsp; if (IE) { // grab the x-y pos.s if browser is IE&nbsp; &nbsp; tempX = event.clientX + document.body.scrollLeft&nbsp; &nbsp; tempY = event.clientY + document.body.scrollTop&nbsp; } else { // grab the x-y pos.s if browser is NS&nbsp; &nbsp; tempX = e.pageX&nbsp; &nbsp; tempY = e.pageY&nbsp; }&nbsp; // catch possible negative values in NS4&nbsp; if (tempX < 0) {&nbsp; &nbsp; tempX = 0&nbsp; }&nbsp; if (tempY < 0) {&nbsp; &nbsp; tempY = 0&nbsp; }&nbsp; return true;}function showCodeLookup(el, divName) {&nbsp; //Hide any lookup tables that are displayed first&nbsp; document.getElementById("divSpec").style.display = "none";&nbsp; var divCodes = document.getElementById(divName);&nbsp; codeEl = el;&nbsp; computeCoordinates();&nbsp; divCodes.style.display = "block";&nbsp; divCodes.style.left = tempX; //(change here)assigning coordinate found&nbsp; divCodes.style.top = tempY; //(change here)}function setCode(divName, code) {&nbsp; var divEl = document.getElementById(divName);&nbsp; codeEl.value = code;&nbsp; divEl.style.display = "none";}function computeCoordinates() {&nbsp; var isIE = document.all ? true : false;&nbsp; var ScrollTop = document.body.scrollTop;&nbsp; var _x = tempX;&nbsp; var _y = tempY;&nbsp; if (ScrollTop == 0) {&nbsp; &nbsp; if (window.pageYOffset)&nbsp; &nbsp; &nbsp; ScrollTop = window.pageYOffset;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;&nbsp; }&nbsp; if (isIE) {&nbsp; &nbsp; _x = tempX + document.body.scrollLeft;&nbsp; &nbsp; _y = tempY + ScrollTop;&nbsp; }&nbsp; tempX = _x;//change here&nbsp;&nbsp; tempY = _y;//change here}function hideThis(id) {&nbsp; document.getElementById(id).style.display = "none";}(上面的代码已经在chrome上进行了测试,并且可以按接受的方式工作)工作代码 :var IE = document.all ? true : falsevar codeEl;// If NS -- that is, !IE -- then set up for mouse captureif (!IE) document.captureEvents(Event.MOUSEMOVE)// Set-up to use getMouseXY function onMouseMovedocument.onmousemove = getMouseXY;// Temporary variables to hold mouse x-y pos.svar tempX = 0; //declare&nbsp;var tempY = 0; //declarevar mouseX = 0;var mouseY = 0;//var frm = document.dashboardSearchForm;function getMouseXY(e) {&nbsp; if (IE) { // grab the x-y pos.s if browser is IE&nbsp; &nbsp; tempX = event.clientX + document.body.scrollLeft&nbsp; &nbsp; tempY = event.clientY + document.body.scrollTop&nbsp; } else { // grab the x-y pos.s if browser is NS&nbsp; &nbsp; tempX = e.pageX&nbsp; &nbsp; tempY = e.pageY&nbsp; }&nbsp; // catch possible negative values in NS4&nbsp; if (tempX < 0) {&nbsp; &nbsp; tempX = 0&nbsp; }&nbsp; if (tempY < 0) {&nbsp; &nbsp; tempY = 0&nbsp; }&nbsp; return true;}function showCodeLookup(el, divName) {&nbsp; //Hide any lookup tables that are displayed first&nbsp; document.getElementById("divSpec").style.display = "none";&nbsp; var divCodes = document.getElementById(divName);&nbsp; codeEl = el;&nbsp; computeCoordinates();&nbsp; divCodes.style.display = "block";&nbsp; divCodes.style.top = tempX +'px'; //(change here)assigning coordinate found&nbsp; divCodes.style.left = tempY +'px'; //(change here)}function setCode(divName, code) {&nbsp; var divEl = document.getElementById(divName);&nbsp; codeEl.value = code;&nbsp; divEl.style.display = "none";}function computeCoordinates() {&nbsp; var isIE = document.all ? true : false;&nbsp; var ScrollTop = document.body.scrollTop;&nbsp; var _x = tempX;&nbsp; var _y = tempY;&nbsp; if (ScrollTop == 0) {&nbsp; &nbsp; if (window.pageYOffset)&nbsp; &nbsp; &nbsp; ScrollTop = window.pageYOffset;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;&nbsp; }&nbsp; if (isIE) {&nbsp; &nbsp; _x = tempX + document.body.scrollLeft;&nbsp; &nbsp; _y = tempY + ScrollTop;&nbsp; }&nbsp; tempX = _x; //change here&nbsp;&nbsp; tempY = _y; //change here}function hideThis(id) {&nbsp; document.getElementById(id).style.display = "none";}.lookupTable{&nbsp; display:none;&nbsp; padding:5px;&nbsp; z-index:10;&nbsp; font-size: 10px;&nbsp;&nbsp; position: absolute;&nbsp; border: 2px solid #933;&nbsp;&nbsp; background-color:white;&nbsp;&nbsp; width: 220px;&nbsp;&nbsp; height:180px;&nbsp;&nbsp;&nbsp;}<html><body><div class="mRow">&nbsp; <label for="SS">Special Subjects:</label>&nbsp; <span class="numLbls">1. </span><input type="text" name="ade" value="<%=ade[0]%>" size="6" maxlength="6" onclick="showCodeLookup(this, 'divSpec')" />&nbsp; <span class="numLbls">2. </span><input type="text" name="ade" value="<%=ade[1]%>" size="6" maxlength="6" onclick="showCodeLookup(this, 'divSpec')" />&nbsp; <span class="numLbls">3. </span><input type="text" name="ade" value="<%=ade[2]%>" size="6" maxlength="6" onclick="showCodeLookup(this, 'divSpec')" /></div><div id="divSpec" class="lookupTable" onClick="hideThis(this.id)">&nbsp; <table>&nbsp; &nbsp;ddddddddd&nbsp; </table></div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript