简体   繁体   English

jQuery-使用Div中的值更改锚标记的href

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

I am having an issue with changing the href value in anchor tag. 我在更改锚标记中的href值时遇到问题。 Basically, I want to use a cluetip which opens a tooltip page based on the employee ID of the user who has logged in. 基本上,我想使用一个提示提示,该提示提示根据已登录用户的员工ID打开工具提示页面。

Cluetip Code: 提示代码:

    $(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"/>'
          });

} }

Now, the anchor tag is seen: 现在,可以看到锚标记:

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

I am trying to change the href link to point to the correct employee ID using jquery.So, I thought of storing the value of employee ID in DIV. 我正在尝试使用jquery更改href链接以指向正确的员工ID。因此,我想到了将员工ID的值存储在DIV中。

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

JQuery to change the href using DIV tag: 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;           


      }


    );

The above code gives me an error saying WorkExp_div does not exist in the current context. 上面的代码给我一个错误,指出当前上下文中不存在WorkExp_div。 I tried by removing the Client ID by doing divObj = document.getElementById('Workexp_Div'); 我尝试通过做divObj = document.getElementById('Workexp_Div');来删除客户端ID。 but then the object is returned as null. 但随后该对象将返回为null。

Could anyone please let me know how to retrieve the value of div tag and change the value of href in tag? 有人可以让我知道如何检索div标签的值并更改标签中href的值吗?

Thanks a lot 非常感谢

If you replace: 如果您更换:

      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 );
              }
          }
      }

with: 与:

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

does that alert and return the proper value? 会发出警报并返回正确的值吗?

IF so, then you could remove that and do: 如果是这样,则可以将其删除并执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM