How to insert an element in a div in XY position

I am trying to create a webpage where users can embed output (e.g. google map) in between text using context menu.

My problem is that I do not know how to insert the pin into the exact position. Using the DOM, I can only get to the nearest DIV (using this ), but the DIV contains a lot of text and the PIN should be located next to the text.

I can get the mouse click position using pageX and pageY, but I don’t know how to insert an element at this position. I tried in jQuery: insertAfter, add, add, but don't get the desired result.

Any idea how to solve this problem.

Regards, choesang tenzin

 $("#Content").rightClick( function(e) {

    $("<div class='pin'/>").insertAfter(this); 
 });    
+3
3

. . "RANGE" (div), , z-. :

  • ,
  • ( )

    $( "div" ). click (function (e) { // FF
      var range = window.getSelection(). getRangeAt (0);   var pin = document.createElement('img');   pin.setAttribute( 'SRC', 'pin_org.png');
      pin.setAttribute( '', '');
      range.insertNode(); });

    $( "img.pin" ). live ('mouseover', function() { ( "!!" ); });

0
$("#Content").rightClick( function(e) {
        var myTop = ...;
        var myRight= ...;
        $("<div class='pin' style='position:absolute; top: ' + myTop +'px; right: ' + myRight + 'px;/>").insertAfter(this); 
 });

, , x y e. , x, y x, y #content.

+2

Set the style position:relaitve;in the containing div so that it becomes a layer. Thus, it works as a source for absolutely positioned elements within it.

0
source

Source: https://habr.com/ru/post/1704273/


All Articles