简体   繁体   English

将HTML置于光标旁边

[英]Position HTML next to cursor

I would like to know how to position a fragment of html to appear next to where the cursor is, specifically on click. 我想知道如何将html片段放置在光标所在的位置旁边,特别是单击时。 So far I have a div which will position itself next to the cursor on click. 到目前为止,我有一个div,它将在点击时将其自身定位在光标旁边。

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

But rather what I am after is to not 'show' any existing div, but insert new code 'on the fly' to where the cursor is. 但是,我要做的是不“显示”任何现有的div,而是“即时”将新代码插入到光标所在的位置。

Thank you 谢谢

Use .html() to insert the text into the div that appears next to your cursor. 使用.html()将文本插入光标旁边显示的div中。

Example: $("#message").html("html text that you want"); 示例: $("#message").html("html text that you want");

If you want to continue to add text to message then use .append() , otherwise .html() will overwrite the contents of #message each time. 如果要继续向消息中添加文本,请使用.append() ,否则.html()每次都会覆盖#message的内容。

You just have to append ( append ) or insert ( html ) the code you want on that div: 您只需要在该div上追加( append )或插入( html )您想要的代码:

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

Full code: 完整代码:

 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: http://jsfiddle.net/zEsF5/3/ 现场演示: http//jsfiddle.net/zEsF5/3/

You can add new elements easily if you do: 您可以轻松添加新元素:

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();
  }
});

There is a working JS Fiddle 有一个正在工作的JS小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 html mailto链接中的游标位置 - Cursor position in html mailto link 单击时在光标位置旁边创建DIV - Create a DIV next to the cursor position on click 更改元素的 position 旁边的 cursor - Change element's position next to the cursor 不带HTML的光标位置需要转换为带HTML的位置 - Position of cursor without HTML needs to be converted to position with HTML 如何在Ckeditor中的光标位置放置一个html元素? - How to place an html element at the cursor position in Ckeditor? 如何获取包含HTML的contentEditable中的光标位置? - How to get the cursor position in contentEditable containing HTML? HTML 文本区域:是否可以更改 cursor position? - HTML Textarea: is it possible to change cursor position? 将 Bootstrap 弹出框放置在光标位置旁边(在单击的元素内部) - Placing Bootstrap popover next to cursor position (inside of clicked element) 在光标位置插入HTML,然后使用javascript将光标移动到插入的html的末尾 - Inserting HTML at cursors position and moving cursor to end of inserted html with javascript 在 contentEditable div 中获取光标位置,打开弹出窗口,然后在光标位置插入新的 html - Get cursor position in contentEditable div, open pop up, then insert new html at cursor position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM