c# - 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);
}
解决方案
您必须使用“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);
})
推荐阅读
- python - 将 dask 数据框应用于 3D 条形图
- traefik - traefik - 拦截来自服务的响应
- python - Discord.py 如何制作干净的对话树?
- node.js - 打开页面后Nodejs selenium进程关闭
- python - Scrapy 从网站返回未更新的数据
- javascript - javascript循环遍历数组并根据符号添加html标签
- javascript - 从数组的可观察对象中获取可观察对象数组
- ios - 我的旧版本支持 Ipad 和 Iphone 应用程序,但现在我只想在应用商店中发布 iphone 版本,这种类型的错误来了
- prometheus - Prometheus 查询 - 指标的平均值,特定时间段
- android - 如何使用 Jetpack compose 更新变量 ~ Kotlin Coroutine