getElementById()dosen't find <input type="hidden" runat="server">

it2022-05-05  148

getElementById()  dosen't find <input type="hidden" runat="server"/>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <input id="divPosition" type="hidden" value="12" runat="server" /> <div id="divScroller" style="overflow:scroll; margin-top:10px; height:505px; width:100%" onscroll="GetDivPosition(this)"> ... ... </div> ... </asp:Content>

onscroll I am calling Javascript function "GetDivPosition(this)" and trying to save the sroll positon in hidden field. But the javascript function doesn't find the hidden field if I have runat="server"  attribute with hidden field. Here is the javascript function

function GetDivPosition(divRef) { //alert(divRef.scrollTop); document.getElementById("divPosition").value = divRef.scrollTop; alert("hidden" + document.getElementById("divPosition").value); }

 

 

A server contorl inside Content page will have some prefix added to its ID,when it is generated as HTML.so you'll have to access by using its ClientID like this.

document.getElementById('<%=divPosition.ClientID%>').value=divRef.scrollTop;

 

注意:使用<%=divPostion.ClientID%>的时候,这段js不能写在单独的文件中,必须写在aspx文件中才可以.

转载于:https://www.cnblogs.com/hongjiumu/archive/2012/09/04/2670036.html


最新回复(0)