首页 > 解决方案 > Link inside Link web 方法

问题描述

嗨,我正在尝试通过锚链接导航。只有第一个链接有效。例如,如果我单击页面中的链接,它会转到该链接,但是当我单击的页面中有另一个链接时,它不起作用。并且第二个具有相同的第一个链接请帮助。

 $('#frmDisplay').on('load', function () {
               $('#frmDisplay a.anchorLink').on('click', function () {
                     var id = $(this).attr('id');
                     var hid = document.getElementById('<%= HiddenField1.ClientID %>');
                     hid.value = id;
                     $.ajax({
                         type: "POST",
                         contentType: "application/json; charset=utf-8",
                         url: "Amm.aspx/getlink",
                         data: "{'Id': '" + id + "'}",
                         dataType: "json",
                         success: function (data) {
                             $('#frmDisplay').contents().find('html').html(data.d);
                         },
                         error: function (response) {
                             alert(response.responseText);
                         }
                     });
                 });
             });
 public static string getlink(int Id)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
            string link = "extlink";
            BookTree obj = new BookTree();
            DataSet ds = obj.getlink(Id);
            SqlCommand cmd=new SqlCommand("select vcFilePath from tblBookNodes where iModuleId='" + Id + "'",conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                bytes = (byte[])dr["vcFilePath"];
            }
            string fileName = link.Replace(" ", "_") + ".htm";
            // DirectoryInfo strPath = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~/Linking/"));
            //string strPath = HttpContext.Current.Server.MapPath(@"/Linking/") + fileName;
            //foreach (FileInfo file in strPath.GetFiles())
            //{
            //  file.Delete();
            //}
            string path = Path.Combine(HttpContext.Current.Server.MapPath("~/htmlFile/"), fileName);
            var doc = new HtmlDocument();
            string html = Encoding.UTF8.GetString(bytes);
            doc.LoadHtml(html);
            StringWriter sw = new StringWriter();
            var hw = new HtmlTextWriter(sw);
            StreamWriter sWriter = new StreamWriter(path);
            sWriter.Write(sw.ToString());
            doc.Save(sWriter);
            sWriter.Close();
            //string fileContents = html;
            //System.IO.File.WriteAllText(path, html);
            return File.ReadAllText(path);
        } 

标签: c#asp.netajax

解决方案


您必须使用“on”而不是 find() 和附加事件侦听器。请参考这个例子:

$('#formDisplay a.anchroLink').on('click', function() {
  // TODO: handle the click
})

这是必要的,因为当您添加事件侦听器时,DOM 尚未准备好处理对不存在内容的请求。使用“on”,您可以将事件附加到类或简单的 DOM 查询。

为了更清楚,我根据我之前的建议发布了您的代码并进行了修改:

$('#frmDisplay a.anchorLink').on('click', function () {
  var id = $(this).attr('id');
  var hid = document.getElementById('<%= HiddenField1.ClientID %>');
  hid.value = id;
  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Amm.aspx/getlink",
    data: "{'Id': '" + id + "'}",
    dataType: "json",
    success: function (data) {
      $('#frmDisplay').contents().find('html').html(data.d)
    },
    error: function (response) {
      alert(response.responseText);
    }
  });
});

我的最后一个答案(我不知道您使用的是 iFrames):

function clickHandler() {
  var id = $(this).attr('id');
  var hid = document.getElementById('<%= HiddenField1.ClientID %>');
  hid.value = id;
  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Amm.aspx/getlink",
    data: "{'Id': '" + id + "'}",
    dataType: "json",
    success: function (data) {                         
      $('#frmDisplay').contents().find('html').html(data.d);
      // You can reload the event handler because the prev one
      // has been lost after refreshing the page with the ajax call.
      $('#frmDisplay').contents()
        .find('a.anchorLink')
        .click(clickHandler);
    },
    error: function (response) {
      alert(response.responseText);
    }
  });
}
$('#frmDisplay').on('load', function () {
  $('#frmDisplay')
    .contents()
    .find('a.anchorLink')
    .on('click', clickHandler);
})

推荐阅读