简体   繁体   中英

change anchor tag href thats loaded through ajax

Basically I have a gridview that I page through the server instead of in memory, the problem is every row in the gridview has a url that I change with javascript, The server control I am using for this colum in the asp:HyperLinkField. The reason this must be done in javascript dynamic is because I must escape the parameter that is appended to url as a query parameter because it could have hash tags in them.So my javascript:

$('#<%=grid1.ClientID%> a').each( function(){
 var url=   $(this).attr("href");
    var parameter =//this line gets the parameter from the url
  url= "Page.aspx?param="+escape(parameter);
  $(this).attr("href", url);
});

This works on first page load with the urls that are initally stored in the gridview, but since I am doing paging on the server and using ajax(asp.net updatepanel) as well, so the next page doesnt have the javascript fired since it doesn't refresh the page. I think jquery live would come into play here, but not sure if thats accurate because I think a event has to be triggered in order to initiate the live handler

No, unfortunately live handlers won't work for changing the actual href. However, if you change your approach somewhat, you can get the same effect.

$('#<%=grid1.ClientID%> a').live('click', function(e) {
    e.preventDefault();
    var url = $(this).attr("href");
    var parameter =//this line gets the parameter from the url
    url = "Page.aspx?param="+escape(parameter);
    window.location = url;
});

So rather than changing the actual attribute on each link, you override the behavior associated with clicking that link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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