【问题标题】:How to get html table position如何获取html表格位置
【发布时间】:2013-05-21 23:51:45
【问题描述】:

我有一个 aspx 页面,该页面有一个如下表

<table id="tbl1" runat="server">
<tr>
<td>
Hello world
</td>
</tr>
</table>

现在我想从后面的代码 (C#) 中获取表格的位置(x,y 坐标)。

有什么方法可以得到吗?

【问题讨论】:

    标签: c# html html-table


    【解决方案1】:

    您可以通过多种方式实现这一目标

    1. 添加一个占位符并在运行时放置您的表格:-

      <asp:placeholder id="PlaceHolder1" runat="server" xmlns:asp="#unknown">
      </asp:placeholder>
      
    2. 在运行时设置 CSS

      style1
      {
      position:absolute;
      left:100px; //Example
      top:150px; //Example
      }
      

      在 CS 代码中,使用

      Table.CssClass = "style1";
      
    3. 您可以使用 Control.Style 属性来设置控件的样式:

      Table tbl = new Table();
      tbl.Style[HtmlTextWriterStyle.Position] = "Absolute";
      tbl.Style[HtmlTextWriterStyle.Top] = "10px";
      
    4. 如果您希望它使用完整的 JavaScript。例如:

      <!DOCTYPE html>
      <head>
      <title>Overlapping tables</title>
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
      
      <style type="text/css" media="all">
      table.first
      {
          position: absolute;
          margin: 0;
          padding: 0;
      
          color: #000000;
          background-color: #ff0000;
          text-align: center;
      }
      
      table.second
      {
          margin: 0;
          padding: 0;
          position: relative;
      
          text-align: center;
          color: #00ff00;
          background-color: transparent;
      }
      
      </style>
      
      <script type="text/javascript"><!--
      
      onload = setPos;
      
      function setPos ()
      {
          var table_1 = new namedTable ('first_table');
          var table_2 = new namedTable ('second_table');
      
          table_2.style.top = table_1.style.top - 1;
          table_2.style.left = table_1.style.left - 1;
      }
      
      
      function namedTable(name)
      {
      
          if (document.getElementById)
          {
              this.obj = document.getElementById(name);
              this.style = document.getElementById(name).style;
          }
          else if (document.all)
          {
              this.obj = document.all[name];
              this.style = document.all[name].style;
          }
          else if (document.layers)
          {
              this.obj = document.layers[name];
              this.style = document.layers[name];
          }
      }
      
      //--></script>
      
      </head>
      <body>
      
      <table class="first" id="first_table">
      <tr>
          <td>
              <h1>TABLE</h1>
          </td>
      </tr>
      </table>
      
      <table class="second" id="second_table">
      <tr>
          <td>
              <h1>TABLE</h1>
          </td>
      </tr>
      </table>
      
      </body>
      </html>
      

    注意事项:

    1) 文档仍然在 w3.org 上验证(CSS 和 HTML)。

    2) 在 Mozilla 和 Internet Explorer 中测试。

    3) 由于习惯,我使用 doc-type xhtml1-strict.dtd,但你应该 为 Mozilla 发布时不要使用此文档类型。使用 HTML 而是过渡 - 或者干脆不放任何标题(这将使 浏览器以 Quirk 模式运行,并且对脚本更宽松 错误)。

    4) 如果你想进一步研究这个话题,这里有一个链接到最 我所知道的精确且轻量级的 Javascript 定位技术:

    http://www.quirksmode.org/

    祝你好运!

    【讨论】:

      【解决方案2】:

      试试这个

      ASPX:

          <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title>Demo</title>
          <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
          <script type="text/javascript">
              function getPosition(elmID) {
                  var elmPosition = $("#"+elmID).position();
                  $("#<%= hdnPosition.ClientID%>").val(elmPosition.left + "," + elmPosition.top);
              }
      
              $(document).ready(function () {
                  $("#<%= btnGetPosition.ClientID %>").click(function () {
                      getPosition("tbl1");
                  });
              });
          </script>
      </head>
      <body>
          <form id="frmMain" runat="server">
          <table id="tbl1" runat="server">
              <tr>
                  <td>
                      Hello world
                  </td>
              </tr>
          </table>
          <asp:HiddenField ID="hdnPosition" runat="server" />
          <asp:Button ID="btnGetPosition" runat="server" Text="Get position" 
              onclick="btnGetPosition_Click"/>
          </form>
      </body>
      </html>
      


      代码隐藏:

      protected void btnGetPosition_Click(object sender, EventArgs e)
      {
          string position = hdnPosition.Value;
      }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 2010-10-14
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 2013-01-13
        • 2019-03-09
        相关资源
        最近更新 更多