Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将HTML放在光标旁边_Javascript_Jquery - Fatal编程技术网

Javascript 将HTML放在光标旁边

Javascript 将HTML放在光标旁边,javascript,jquery,Javascript,Jquery,我想知道如何定位html片段以显示在光标所在位置的旁边,特别是在单击时。到目前为止,我有一个div,它会在单击时将自身定位在光标旁边 shape.on('click', function(event){ if (event.which == 1) { $("#message").css({ top: event.pageY + 5, left: event.pageX + 5 }

我想知道如何定位html片段以显示在光标所在位置的旁边,特别是在单击时。到目前为止,我有一个div,它会在单击时将自身定位在光标旁边

 shape.on('click', function(event){ 
   if (event.which == 1) {
          $("#message").css({
                top: event.pageY + 5,
                left: event.pageX + 5
            }).show();
   }
});
但我要做的是不要“显示”任何现有div,而是在光标所在的位置“动态”插入新代码。

谢谢

使用
.html()
将文本插入光标旁边的div中

示例:
$(“#message”).html(“您想要的html文本”)

如果要继续向消息中添加文本,请使用
.append()
,否则
.html()
每次都会覆盖
#消息的内容。

您只需在该div上添加()或插入()所需的代码即可:

$('#message').html('Hello world!');
完整代码:

 shape.on('click', function(event){ 
   if (event.which == 1) {
          $("#message").css({
                top: event.pageY + 5,
                left: event.pageX + 5
            }).show();

          $('#message').html('Hello world!');
   }
});

Living demo:

如果您执行以下操作,则可以轻松添加新元素:

shape.on('click', function(event){ 
  if (event.which == 1) {
    $("<div>The <strong><em>HTML</em> code</strong> you want<br/>can go here<div>").css({
           top: event.pageY + 5,
           left: event.pageX + 5,
           position: 'absolute'
    }).appendTo(shape).show();
  }
});
shape.on('click',函数(事件){
if(event.which==1){
$(HTML代码
可以转到这里。)css({ 顶部:event.pageY+5, 左:event.pageX+5, 位置:'绝对' }).appendTo(shape.show(); } });

有一个有效的

,所以您想使用
$(“#message”).html(“您想要的html文本”)
?或者
.text()
实际上是的,可以。我把事情复杂化了。谢谢,杰克。是的,我会发帖回答你的问题