diff --git a/js/widgets/popup.js b/js/widgets/popup.js index c570b5b7dc6..a6727d62904 100644 --- a/js/widgets/popup.js +++ b/js/widgets/popup.js @@ -298,12 +298,10 @@ $.widget( "mobile.popup", { if ( targetElement !== ui.container[ 0 ] ) { target = $( targetElement ); - if ( 0 === target.parents().filter( ui.container[ 0 ] ).length ) { - $( this.document[ 0 ].activeElement ).one( "focus", function(/* theEvent */) { - if ( targetElement.nodeName.toLowerCase() !== "body" ) { - target.blur(); - } - }); + if ( !$.contains( ui.container[ 0 ], targetElement ) ) { + $( this.document[ 0 ].activeElement ).one( "focus", $.proxy( function() { + this._safelyBlur( targetElement ); + }, this ) ); ui.focusElement.focus(); theEvent.preventDefault(); theEvent.stopImmediatePropagation(); @@ -634,13 +632,28 @@ $.widget( "mobile.popup", { } }, + _safelyBlur: function( currentElement ){ + if ( currentElement !== this.window[ 0 ] && + currentElement.nodeName.toLowerCase() !== "body" ) { + $( currentElement ).blur(); + } + }, + _openPrerequisitesComplete: function() { - var id = this.element.attr( "id" ); + var id = this.element.attr( "id" ), + firstFocus = this._ui.container.find( ":focusable" ).first(); this._ui.container.addClass( "ui-popup-active" ); this._isOpen = true; this._resizeScreen(); - this._ui.container.attr( "tabindex", "0" ).focus(); + + // Check to see if currElement is not a child of the container. If it's not, blur + if ( !$.contains( this._ui.container[ 0 ], this.document[ 0 ].activeElement ) ) { + this._safelyBlur( this.document[ 0 ].activeElement ); + } + if ( firstFocus.length > 0 ) { + this._ui.focusElement = firstFocus; + } this._ignoreResizeEvents(); if ( id ) { this.document.find( "[aria-haspopup='true'][aria-owns='" + id + "']" ).attr( "aria-expanded", true ); @@ -725,8 +738,6 @@ $.widget( "mobile.popup", { var container = this._ui.container, id = this.element.attr( "id" ); - container.removeAttr( "tabindex" ); - // remove the global mutex for popups $.mobile.popup.active = undefined; diff --git a/tests/integration/popup/index.html b/tests/integration/popup/index.html index 5e7a4e8a836..cf2de27a71e 100644 --- a/tests/integration/popup/index.html +++ b/tests/integration/popup/index.html @@ -54,6 +54,7 @@

This is another test popup

Pop + Pop