我正在使用ASPX2010,其中一个SharePoint页面包含一个记事板web部件,它提供了在给定页面上留下评论的简单功能。
当页面加载时,该web部件使用AJAX检索注释,我无法控制AJAX调用何时完成。
我正在尝试在web部件用于注释的每个表数据标记中插入一个链接或一些文本。
<td class="socialcomment">Comment 1</td>
<td class="socialcomment">Comment 2</td>
<td class="socialcomment">Comment 3</td>
至
<td class="socialcomment">Comment 1 <a href="#">Report inappropiate</a></td>
<td class="socialcomment">Comment 2 <a href="#">Report inappropiate</a></td>
<td class="socialcomment">Comment 3 <a href="#">Report inappropiate</a></td>
像这样使用JQuery
$('td.socialcomment').append(' <a href="#">Report inappropiate</a>');
我无法在上面的场景中使用JQuery的live()函数(适用于其他场景),但我认为这是因为它是为事件而设计的。我也尝试过.ajaxComplete(),但我认为它根本不起作用,因为我无法控制Ajax调用,或者SharePoint执行的Ajax调用没有在JQuery中注册。
任何帮助或见解都是非常感谢的。提前感谢!
发布于 2011-09-25 04:53:46
我当然想学习一种比这更好的方法--你可以使用setInterval
周期性地检查DOM中与选择器匹配的元素,以及你还没有“处理”的元素,并添加你的链接到它们并将它们标记为“已处理”。不过,我对性能并不是很乐观。
类似于:
setInterval(processComments, 250);
//...
function processComments() {
$('td.socialcomment:not(.processed)').append(' <a href="#">Report inappropiate</a>').addClass('processed');
}
这是一个fiddle。
更新
根据这个jQuery forum thread,liveQuery插件显然可以让您指定在向DOM添加新元素时将执行的函数(如果我理解正确的话,可以使用jQuery的DOM操作方法)。
发布于 2011-09-25 05:44:19
如果Sharepoint中的ajax调用是使用jQuery的ajax框架进行的,那么您可以注册一个global ajaxComplete handler,它将在任何ajax调用完成时被调用。不过,这只适用于使用jQuery进行的ajax调用。
发布于 2011-12-30 14:20:11
我也面临着同样的问题,我可以像这样使用live函数。
$(window).load(function() {
$('span.socialcomment-username').find("a").live({
click: function() {
alert("asd");
},
mouseover: function() {
$(this).removeAttr('href');
$(this).removeAttr('onclick');
},
mouseout: function() {
$(this).removeClass("over");
}
});
});
https://stackoverflow.com/questions/7541788
复制相似问题