开发者

Getting width of an existing div with jQuery returns null

开发者 https://www.devze.com 2023-01-08 04:18 出处:网络
I have an ASP.NET reportviewer in a page whose开发者_JAVA技巧 width (a div width) I am trying to determine.

I have an ASP.NET reportviewer in a page whose开发者_JAVA技巧 width (a div width) I am trying to determine. I have the test code below. The first alert returns the proper client id. I can see the div in the html source. I can see its width in FireBug. However the second alert returns null. The syntax looks fine. Why is it returning null?

<script type="text/javascript" src="includes/jquery-1.4.2.min.js"></script>
<script type="text/javascript" language="javascript">
alert('<%=rvMain.ClientID %>');
alert( $('#<%=rvMain.ClientID %>').width() );

</script>


Have your elements loaded at the time that javascript fires? Try using a document.ready call and see if you get the same result.


Are you wrapping that code in $(document).ready(function () { /* code */ });? If not, you may be trying to reference an element that doesn't yet exist in the DOM.


However the answers above are correct but if you are not using jquery then you can use a trick which is to do the javascript calling at the end of the document.

this happens because the order in which the files are received from server are different and javascript execution happens as soon as it loads

0

精彩评论

暂无评论...
验证码 换一张
取 消