【问题标题】:Link inside Link web methodLink inside Link web 方法
【发布时间】:2021-11-19 18:51:23
【问题描述】:

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

 $('#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.net ajax


    【解决方案1】:

    您必须使用“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);
    })
    

    【讨论】:

    • 它不工作。如果单击时使用,则无法导航到第一个链接本身
    • 请帮助它不工作
    • 抱歉耽搁了,您可以发布示例实现的代码或链接您的代码吗?
    • 这是正在发生的事情imgur.com/5IOJtod。请帮忙
    • 抱歉,我需要查看实现的代码,而不是结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多