Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the jQuery search scope correctly?

I've stumbled up this code and I'm wondering if its just limiting the scope or selecting both of the elements at the same time.

container = jQuery("#test", parent.document);

jQuery("param[name=scale]", another.object)

Can anyone shed some light on this?

Edit: the full example:

<script type="text/javascript">
jQuery = parent.jQuery;
container = jQuery("#test", parent.document);

another = {
    targetw: container.width()
};

function onEnd(){
  container.slideUp();
}

function onStart(){
  another.object = jQuery("object", document);

  another.w =  another.object.attr("width");
  another.h =  another.object.attr("height");
  another.targeth = Math.floor(another.targetw * another.h  / another.w);

  jQuery("div>iframe",container).width(another.targetw);
  jQuery("div>iframe",container).height(another.targeth);

  another.object.css("width", another.targetw+"px");
  another.object.css("height", another.targeth+"px");

  another.object.attr("width", another.targetw);
  another.object.attr("height", another.targeth);

  jQuery("param[name=scale]",another.object).attr("value","exactfit");

  another.object.parent().attr('style', function(i,s) { return s + 'background:none; width: '+another.targetw+'px !important; height: '+another.targeth+'px !important;' });

}
document.write('*snipped code*');
</script>

`

like image 779
wadge Avatar asked Feb 10 '23 05:02

wadge


2 Answers

That appears to be the context argument from the jQuery selector jQuery(selector[,context]).

From http://api.jquery.com/jquery/

Selector Context

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, to do a search within an event handler, the search can be restricted like so:

$( "div.foo" ).click(function() {
  $( "span", this ).addClass( "bar" );
});

When the search for the span selector is restricted to the context of this, only spans within the clicked element will get the additional class.

Internally, selector context is implemented with the .find() method, so $( "span", this ) is equivalent to $( this ).find( "span" ).

like image 124
j08691 Avatar answered Feb 12 '23 22:02

j08691


It is a child - parent selector. The search starts within the parent instead of the whole DOM.

$('childNode', parent)

It is same as

$(parent).find('childNode')

Considering container = jQuery("#test", parent.document);

As #test is an ID based selector just jQuery('#test') wont make any difference as IDs are unique across the DOM elements.

like image 23
Shaunak D Avatar answered Feb 12 '23 21:02

Shaunak D