开发者

How can I order a jQuery wrapped set?

开发者 https://www.devze.com 2023-01-28 10:01 出处:网络
I need to be able to get the top-most \".ui-dialog\" element in a wrapped set. My first thought was to do something like this:

I need to be able to get the top-most ".ui-dialog" element in a wrapped set.

My first thought was to do something like this:

var topDialog = $(".ui-dialog").orderBy("[style*=z-index]").eq(0);

Is there a nicer way to do this than writing a loop to check the values?

Edit: Just to clarify... I need to be able to get the开发者_高级运维 top-most element at any given point during the life of the page (after multiple dialogs are opened and closed). Nick Craver's answer to use the maxZ variable doesn't seem to work since the variable is not decremented when dialogs are removed.

Here's the loop I'm using now when I close a dialog that seems kind of ugly to me:

// Enable printing of the top-most dialog or #page
if ($(".ui-dialog").length > 0) {

    var top = $(".ui-dialog").first();

    $(".ui-dialog").each(function () {
        if ($(this).css("z-index") > top.css("z-index")) {
            top = $(this);
        }
    });

    top.removeClass("dont-print");

} else {

    $("#page").removeClass("dont-print");

}


Since the dialogs are made to stack, the variable of the top z-index is actually maintained and accessible, it's: $.ui.dialog.maxZ, you can see how it's set here.

So instead of ordering, you can just see which dialog has that highest index (only one will), like this:

var topDialog = $(".ui-dialog").filter(function() { 
                  return $(this).css("z-index") == $.ui.dialog.maxZ;
                });

You can test it out here.


You can always make a custom selector too :)

jQuery.expr[':'].highestModal = function(node) {
    return $(node).css('z-index') == $.ui.dialog.maxZ;
};

Borrowing from Nick Craver's answer (I hope he doesn't mind :P Have an upvote! )

Use it like so...

$('.ui-dialog:highestModal');


    function clearUiMaxZ() {
        setTimeout(function() {
            $.ui.dialog.overlay.maxZ = 0;
            $.ui.dialog.maxZ = 0;
        }, 100);
    }

Call this function on Dialog Close , this will reset the maxZ.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号