Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery is not functional? [duplicate]

firefox (v52.0), jquery

this works:

// example 1
$('html').find('body')

this works:

// example 2
var h
h=$('html')
h.find('body')

This doesn't work:

// example 3
var f
f=$('html').find
f('body')

I get

Error: Permission denied to access property "ownerDocument"

why?

but this works:

// example 4
var a
a = x => $('html').find(x)
a('body')
like image 484
Jasen Avatar asked Mar 29 '26 20:03

Jasen


1 Answers

Example 3 doesn't work because find is called on the global context when you assign it to f. If you use call and pass in a valid jQuery object as the context, the code works. Try this

var f = $('html').find;
console.log(f.call($('html'), 'body').length)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Also, example 4 works because a can be translated to the following code, if written without an arrow function.

var a = function(x) {
  return $('html').find(x);
};

It's just example 1, but with a wrapper function in order to take a parameter

like image 92
maazadeeb Avatar answered Apr 02 '26 02:04

maazadeeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!