【问题标题】:Get Co-ordinates on page load in asp.net在 asp.net 中获取页面加载时的坐标
【发布时间】:2012-11-19 08:51:49
【问题描述】:

我有一个情况。我正在使用 Windows(ASP.NET)的 Web 应用程序。当我的网页出现时,在页面加载时,它会动态生成 ImageButtons。我需要确定页面上每个 ImageButton 控件的 X、Y 或 TOP、LEFT 坐标,而无需单击任何 ImageButtons。我需要在单击一个 ImageButton 时在一些 ImageButtons 之间画一条线。

【问题讨论】:

    标签: c# javascript asp.net


    【解决方案1】:

    尝试使用一些 Javascript 代码或 jQuery。请在下面找到 jQuery 代码

    这里我使用了 position() 方法,它可以为您提供元素的左侧和顶部位置。您可以根据需要更改selector

    更新:假设您有像<div id='images-container'> .. all images here </div> 这样的HTML,并使用each() 方法更新了下面的选择器

    您也可以使用offset()

     <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
     <script language="javascript" type="text/javascript">
        $(document).ready(function () {
             $('#images-container img').each(funcion(){
             var coordinates=$(this).position();
             coordinates.left;// gives you x
             coordinates.top;// gives you y
             //save these values in some hidden field and access it in code behind if needed
           });
        });
     </script>
    

    更多信息请参考这篇文章jQuery x y document coordinates of DOM object

    【讨论】:

    • @Murli:谢谢 Murli。这是否给了我我们表单上的每个 ImageButton 的坐标?以及如何在我的 C# 代码中使用这些值
    • 这段代码给出了第一个图像按钮。如果要获取所有图像按钮,只需使用图像选择器。 $('#images-container img').position() -> 这给出了任何容器内的所有图像,例如具有 id='images-container' 的 div
    • @Murli:对于纯粹动态生成的 ImageButtons,我该如何做? .aspx 中没有 div。一切都在 .CS 文件中生成。如何在 .cs 文件中应用 abive 代码。如何在.CS文件中使用jquery。?请帮忙。
    猜你喜欢
    • 1970-01-01
    • 2016-05-11
    • 2019-07-19
    • 1970-01-01
    • 2011-08-05
    • 2011-07-08
    • 2017-05-22
    • 2011-11-13
    • 1970-01-01
    相关资源
    最近更新 更多