簡體   English   中英

jQuery-使用Div中的值更改錨標記的href

[英]JQuery- Change href of anchor tag using value in Div

我在更改錨標記中的href值時遇到問題。 基本上,我想使用一個提示提示,該提示提示根據已登錄用戶的員工ID打開工具提示頁面。

提示代碼:

    $(document).ready
    (
      function () {

          $('#sticky').cluetip({ sticky: true, closePosition: 'title', arrows: true });
          $('a.jt:eq(0)').cluetip({
              cluetipClass: 'jtip',
              arrows: true,
              dropShadow: false,
              hoverIntent: false,
              sticky: true,
              mouseOutClose: true,
              closePosition: 'title',
              closeText: '<img src="Images/cross.png" alt="close"/>'
          });

}

現在,可以看到錨標記:

     <a class="jt" style="color: #FFFFFF;" id="Anchor_work"  runat="server" href='WorkExp.aspx?Emplid=12345'>Previous Work Experience</a>

我正在嘗試使用jquery更改href鏈接以指向正確的員工ID。因此,我想到了將員工ID的值存儲在DIV中。

    <div id="Workexp_div" runat="server"/>

jQuery使用DIV標簽更改href:

      $(document).ready
    (
      function () {

          $('#sticky').cluetip({ sticky: true, closePosition: 'title', arrows: true });
          $('a.jt:eq(0)').cluetip({
              cluetipClass: 'jtip',
              arrows: true,
              dropShadow: false,
              hoverIntent: false,
              sticky: true,
              mouseOutClose: true,
              closePosition: 'title',
              closeText: '<img src="Images/cross.png" alt="close"/>'
          });

          function showDivText() {

              //divObj = document.getElementById('Workexp_Div'); 
              divObj = document.getElementById("#<%= Workexp_Div.ClientID %>");
              if (divObj) {
                  if (divObj.textContent) { // FF
                      alert(divObj.textContent);
                  } else {  // IE           
                      alert(divObj.innerText);  //alert ( divObj.innerHTML );
                  }
              }
          }
          //var new_href = "WorkExp.aspx?Emplid=" + $('#ctl00_ContentPlaceHolder1_Workexp_div').InnerText;
          var new_href = "WorkExp.aspx?Emplid=" + showDivText();
          $("#Anchor_work").attr("href", new_href);
          //= 'WorkExp.aspx?Emplid='+ $('#ctl00_ContentPlaceHolder1_Workexp_div').InnerText;           


      }


    );

上面的代碼給我一個錯誤,指出當前上下文中不存在WorkExp_div。 我嘗試通過做divObj = document.getElementById('Workexp_Div');來刪除客戶端ID。 但隨后該對象將返回為null。

有人可以讓我知道如何檢索div標簽的值並更改標簽中href的值嗎?

非常感謝

如果您更換:

      function showDivText() {

          //divObj = document.getElementById('Workexp_Div'); 
          divObj = document.getElementById("#<%= Workexp_Div.ClientID %>");
          if (divObj) {
              if (divObj.textContent) { // FF
                  alert(divObj.textContent);
              } else {  // IE           
                  alert(divObj.innerText);  //alert ( divObj.innerHTML );
              }
          }
      }

與:

function showDivText() {
    var divObj = $("#<%= Workexp_Div.ClientID %>");
    alert(divObj.text());
    return divObj.text();
}

會發出警報並返回正確的值嗎?

如果是這樣,則可以將其刪除並執行以下操作:

var new_href = "WorkExp.aspx?Emplid=" +  $("#<%= Workexp_Div.ClientID %>").text();

暫無
暫無

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

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