简体   繁体   中英

Javascript how can I get a particular child from an object/element returned by elementFromPoint()?

Is there a way to retrieve the child element from an element/object returned by the function elementFromPoint(x, y);

Suppose I have the following statement,

var elem = document.elementFromPoint(x, y);

And lets assume that the element returned and stored in variable elem would have the following structure:

<section class="wrapper">
      some unwraped text
      <div class="hidden">
           <img />
      </div>
</section>

Is there away to retrieve the the img tag from this section wrapper? I am looking to retrieve it and clone it using jQuery and further add the clone to another element. The div wrapping the image tag has a css property of display: none; which will hide the image, I would like to sort of copy that image and reveal it inside some other element.

However I need to find a way to retrieve the img tag before I can clone it and append it further.

EDITED

So this is how I am trying to use the elem object, I need the child img out of it rather than the whole returned element

  $('#unique > img').remove();
  $('#unique').append($(elem > child)
        .clone()
        .children()
        .end());

I believe I don't need the children() function if I can get the right element right up.

Thank ...

What does elem end up being? The <section> ?

$(elem).find('.hidden img')

One of its children?

$(elem).closest('.wrapper').find('.hidden img')

Check if this works:

$("img",$(document.elementFromPoint(x, y)))

or

$(document.elementFromPoint(x, y)).find("img")
var elem = document.elementFromPoint(x, y);
var imageElement = $(elem).find("div.hidden img")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM