简体   繁体   English

更改通过Ajax加载的锚标签href多数民众赞成

[英]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. 基本上,我有一个通过服务器而不是在内存中分页的gridview,问题是gridview中的每一行都有一个用javascript更改的url,我在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: 必须在javascript动态中完成此操作的原因是因为我必须转义作为查询参数附加到url的参数,因为它可能包含哈希标签。因此,我的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. 这可以在第一页加载时使用最初存储在gridview中的URL进行工作,但是由于我正在服务器上进行分页并也使用ajax(asp.net updatepanel),因此下一页没有触发javascript,因为它没有不会刷新页面。 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 我认为jQuery live将在这里发挥作用,但不确定那是否准确,因为我认为必须触发一个事件才能启动实时处理程序

No, unfortunately live handlers won't work for changing the actual href. 不,不幸的是, live处理程序无法更改实际的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. 因此,您无需更改每个链接上的实际属性,而是可以覆盖与单击该链接相关的行为。

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

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