Explain how to test if element attached to main DOM#725
Explain how to test if element attached to main DOM#725Enelar wants to merge 1 commit intojquery:mainfrom
Conversation
|
Thank you for your pull request. It looks like this may be your first contribution to a jQuery Foundation project, if so we need you to sign our Contributor License Agreement (CLA). 📝 Please visit http://contribute.jquery.org/CLA/ to sign. After you signed, the PR is checked again automatically after a minute. If there's still an issue, please reply here to let us know. If you've already signed our CLA, it's possible your git author information doesn't match your CLA signature (both your name and email have to match), for more information, check the status of your CLA check. |
|
Thank you for the PR, you made a good point. I have a few suggestions to improve it. I'd move this paragraph as the first of your PR to explain the potential issue:
|
|
I was thinking that it might worth to show an example.
var span = $( "span" );
var fragment = document.createDocumentFragment();
console.log( span.length ); // 1
fragment.appendChild( span.get( 0 ) );
console.log( span.length ); // Still 1!
console.log( $( "span" ).length ); // 0. The element was detachedAny thoughts? |
|
@AurelioDeRosa it may confuse if you don't use $('body').html('<div><a></a></div>');
var element = $('body a');
console.log(element.length); // 1
console.log(element.parents().length); // 3 div, body, html
$('body').html('');
console.log(element.length); // 1
console.log(element.parents().length); // 1 div |
|
Hi @Enelar. I'm OK with your example too as far as we have one to clarify the concept. |
Since DocumentFragment era
.lengthdo not provide accurate answer to that question in several scenarios.