javascript - 使用 jQuery 自动调整所有对话框的大小

标签 javascript jquery dialog

我在 Stackoverflow 上的另一篇文章中找到了一些代码,用于在 IE 中自动调整 jQuery 对话框的大小,并确保它们不会简单地调整到屏幕的宽度......即使在 IE7 上,它也能很好地工作!

$("#item-popup").dialog({
    autoOpen: false,
    resizable: false,
    autoResize: true,
    width: 'auto',
    modal: true
}).bind("dialogopen", function(event, ui) {
    // only run on IE
    if ($.browser.msie) {
        // fix for width:auto in IE  
        var contentWidth = $(this).width();
        $(this).parent().find('.ui-dialog-titlebar').each(function() {
            $(this).width(contentWidth);
        });
    }
}).bind("dialogclose", function(event, ui) {
    // only run on IE
    if ($.browser.msie) {
        //fix for width:auto in IE 
        $(this).parent().css("width", "auto");
    }
});

我现在想做的是将相同的代码应用于我页面上的所有其他对话框,而不必一遍又一遍地重复它。我已经尝试了一系列逻辑变体,但我似乎无法做到正确。我可以触发dialogopen代码,但它根本不会像我将它直接绑定(bind)到对话框时那样调整对话框的大小。

$('.ui-dialog').bind("dialogopen", function(event, ui) {
    // only run on IE
    if ($.browser.msie) {
        // fix for width:auto in IE  
        var contentWidth = $(this).width();
        $(this).parent().find('.ui-dialog-titlebar').each(function() {
            $(this).width(contentWidth); // <-- Does not resize the dialog
        });
    }
}).bind("dialogclose", function(event, ui) {
    // only run on IE
    if ($.browser.msie) {
        //fix for width:auto in IE 
        $(this).parent().css("width", "auto");
    }
});

经过多次故障排除后,我发现问题出在获取 contentWidth 的代码行中。当直接绑定(bind)到对话框时,它仅返回对话框的宽度(即 435),但是当我将它放在这个通用处理程序中时,它返回窗口宽度(即 1150)。

我在通用 ui-dialog 处理程序中是否错误地引用了对话框?

var contentWidth = $(this).width();

最佳答案

是的,您是对的,您的引用资料不正确。为了绑定(bind)到相同的对话框元素,您应该引用 $(".ui-dialog-content").bind("dialogopen", ...

关于javascript - 使用 jQuery 自动调整所有对话框的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436783/

相关文章:

javascript - 如何在YUI3中使用Assert.isUndefine()和Assert.isNotUndefine()?

javascript - 使用 Angular Universal 延迟加载外部 JS 文件

javascript - 甜蜜警报 2 和 javascript :How do i make a Sweet alert button that sends me to a certain webpage when clicked

javascript - 在 textarea 外部单击会丢失插入符号位置

c++ - 设置了 OFN_ALLOWMULTISELECT 标志的 GetOpenFileName()

Java - 全局、可重用的加载对话框

c++ - 键盘制表符停止在 Windows GUI 上工作

javascript - 如何使用promise.all使用mysql insert来级联样式?

javascript - 以编程方式将输入类型 ="file"值设置为 base64 图像?

jquery - 在 TypeScript 中使用 jQuery $ 前缀