Question

This may seem like a noob question, but I can't seem to do anything about it. I know quite some javascript and I'm been trying this JQuery plugin which checks if two elements overlap. http://jquer.in/helpful-jquery-plugins-for-html5-websites/overlaps/ I have this code:

$('#elm1').overlaps('#elm2')

The thing is how do I get the value from it. When a try an alert with it in a variable I get [object Object] and when I put it in a function I just get the code. Thanks

Était-ce utile?

La solution

Well, it's not so noob question, as this plugin returns value in a non-trivial way:

The second mode is to compare one set of elements against another and return only the elements that overlap.

$('#div1').overlaps('#div2');

In this example, if #div1 and #div2 overlap, the returned jQuery object will have both of them. If not, only #div1.

So the solution is to check the length of the plugin's resulting object - and compare it with sum of lengths of the compared jQuery objects. If these are the same, the elements overlap; if not, they don't. )

In this particular example, you can just compare result's length with 2, like this:

if ( $('#div1').overlaps('#div2').length === 2 ) { // they overlap }

... as in valid DOM there can be exactly one element with a given ID.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top