[英]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();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.