簡體   English   中英

動態找到一個按鈕的位置是asp.net?

[英]Dynamically find the position of a button is asp.net?

我已經編寫了一個JavaScript函數來檢索按鈕的位置並將其分配給asp:updateprogress,但我想將按鈕的位置應用於代碼中的div元素或updateprogress中的標簽控件,以不更新進度。

<asp:UpdateProgress ID="UpdateProgress2"
                    runat="server"
                    AssociatedUpdatePanelID="SendMailUpdatePanel"
                    DisplayAfter="0">
    <ProgressTemplate>
        <div id="blur" style="top: 0px; left: 0px; width: 99%; height: 5000px; position: absolute;background-color: white; z-index: 999; filter: alpha(opacity=50); opacity: 0.5;-moz-opacity: 0.85; padding-top: 25%; padding-left: 30%;" />
        <div id="progress" style="text-align: center; width: 444px; border: 1px solid black;padding: 10px; background-color: #eee; z-index: 998; filter: alpha(opacity=500);-moz-opacity: 1.00;">
            <b>Mail is being Sent! Please Wait...</b>
            <br />
            <asp:Image ID="LoadImage"
                       runat="server"
                       ImageUrl="~/Images/spinner.gif" />
            <br />
        </div>
    </ProgressTemplate>
</asp:UpdateProgress>

我的JavaScript函數是:

function getPosition(btnSendResume, progress)
{
  var btnSendRe = document.getElementById(btnSendResume);
  var divp = document.getElementById(progress);
  divp.style.display="block";
  divp.style.left=btnSendRe.offsetLeft;
  divp.style.top=btnSendRe.offsetTop + btnSendRe.offsetHeight - 40;
}

我在按鈕單擊下面寫了以下內容:

btnSendResume.Attributes.Add("onclick", "getPosition('" + btnSendResume.ClientID + "','" + UpdateProgress2.FindControl(progress).ClientID + "');");

但是這給人的錯誤是在當前環境下不存在progress

您的<div id="progress"是普通的HTML元素,而不是服務器端控件。
您應該只編寫document.getElementById("progress")

您可以通過Jquery做到這一點。

一個簡單的offset()將返回控件的左和頂部位置。

function getPosition(btnSendResume, progress)
   {
     var btnSendReOffset = $('#btnSendResume').offset();
     var btnSendRe = $('#btnSendResume');
     var divp = document.getElementById(progress);
     divp.style.display="block";
     divp.style.left=btnSendReOffset.left;
     divp.style.top=btnSendReOffset.top+ btnSendRe.height() - 40;
  }

您可以在窗口加載時向按鈕添加click事件並觸發功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM