不显示我在回发后右键单击 JavaScript 时显示的列表
Doesn't show list that I show with right click in JavaScript after postback
当用户右键单击 GridView 的行并且 GridView 位于 UpdatePanel 中时,我使用以下 JavaScript 代码显示列表。发生回发时,它不显示列表。
$(document).ready(function () {
//--------------- Info Box --------------------
$('.CityItem').mouseup(function (e) {
if ( e.which === 3 ) {
$('.CityItem').children('.CompanyList').hide();
$(this).children('.CompanyList').toggle();
$(this).children('.CompanyList').css('top', e.pageY - 300 );
$(this).children('.CompanyList').css('left', e.pageX - 175 );
}
e.preventDefault();
});
$('.CityItem').hover( function () {
$(this).children('.CompanyList').hide();
});
});
// Attach a handler to the load event.
Sys.Application.add_load(applicationLoadHandler);
将处理程序(上面的代码)放在 updatePanel.然后 applicationLoadHandler 将在更新面板刷新时被执行。
function applicationLoadHandler() {
//--------------- Info Box --------------------
$(document).on('mouseup', '.CityItem', function (e) {
if ( e.which === 3 ) {
$('.CityItem').children('.CompanyList').hide();
$(this).children('.CompanyList').toggle();
$(this).children('.CompanyList').css('top', e.pageY - 300 );
$(this).children('.CompanyList').css('left', e.pageX - 175 );
}
e.preventDefault();
});
$(document).on('hover', '.CityItem',function () {
$(this).children('.CompanyList').hide();
});
}
这是因为元素在
完成 ajax 调用后再次添加,但 document.ready 不会再次触发。这可以通过事件委派来解决。
$(document).ready(function () {
//--------------- Info Box --------------------
$(document).on('mouseup', '.CityItem', function (e) {
if ( e.which === 3 ) {
$('.CityItem').children('.CompanyList').hide();
$(this).children('.CompanyList').toggle();
$(this).children('.CompanyList').css('top', e.pageY - 300 );
$(this).children('.CompanyList').css('left', e.pageX - 175 );
}
e.preventDefault();
});
$(document).on('hover', '.CityItem',function () {
$(this).children('.CompanyList').hide();
});
});
委托事件的优点是它们可以处理来自 稍后添加到文档中的后代元素。由 选取在 附加了委托事件处理程序,您可以使用委托事件来 避免频繁附加和删除事件处理程序的需要,jQuery API
您还可以使用 Sys.WebForms.PageRequestManager endRequest Event 在 ajax 请求结束时将事件重新绑定到添加的元素。
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)
function EndRequestHandler(sender, args)
{
if (args.get_error() != undefined)
{
bindEvents();
}
}
function bindEvents()
{
//--------------- Info Box --------------------
$('.CityItem').mouseup(function (e) {
if ( e.which === 3 ) {
$('.CityItem').children('.CompanyList').hide();
$(this).children('.CompanyList').toggle();
$(this).children('.CompanyList').css('top', e.pageY - 300 );
$(this).children('.CompanyList').css('left', e.pageX - 175 );
}
e.preventDefault();
});
$('.CityItem').hover( function () {
$(this).children('.CompanyList').hide();
});
}
$(document).ready(function () {
bindEvents();
});
相关文章:
- 如果函数返回True,则显示Javascript按钮
- 背景图像前显示Javascript警报
- 为我的rails应用程序显示javascript弹出消息
- 如何在Javascript中显示Javascript变量
- 如何在colorbox中显示JavaScript变量
- Xamarin iOS WKWebView显示JavaScript警报
- GoogleMapsV3api只显示Javascript循环中的一些标记
- 如何在Asp.net标签控件中显示Javascript函数值
- 如何在对话框中显示javascript变量
- 在asp.net页面中显示javascript执行过程中的加载图标
- 在html中显示javascript变量won'不起作用
- 如何使用按钮在对话框上显示 Javascript 函数的结果
- 打开新窗口进行打印时不显示 JavaScript 图像
- 显示javascript控制台中的消息
- 在加载html网页时显示JavaScript表
- 在Notepad++中更好地突出显示Javascript语法
- 通过php显示javascript内容
- 文本字段未显示javascript函数中的变量
- 显示Javascript表单验证消息
- VB.NET-WebBrowser没有't在html源代码中显示javascript创建的html元素