[英]removeAttr() and remove() doesn't work
I want to use removeAttr()
and remove()
to remove a <div>
, but seems like it doesn't work. 我想使用removeAttr()
和remove()
删除<div>
,但似乎不起作用。 Since I have $('#div1').removeAttr('style').remove(); 由于我有$('#div1')。removeAttr('style')。remove(); in the last line of the javascript, shouldn't that the <div>
only appear just a moment? 在javascript的最后一行中,难道<div>
只出现片刻? I am not sure if javascript is event-driven programming or top-down programming? 我不确定javascript是事件驱动编程还是自顶向下编程? Or depends on the code. 还是取决于代码。
I want to do that because I want to delete and clear the <div>
every time before I drag a new <div>
, and I try to put that line of code everywhere, it doesn't work. 我想这样做,因为我想删除并清除<div>
每一次之前,我将一个新<div>
我试图把该行的代码随处可见,这是行不通的。
I am not a computer science major, please forgive my ignorance. 我不是计算机科学专业的学生,请原谅我的无知。 But I don't understand why the last line doesn't execute? 但是我不明白为什么最后一行没有执行? If you can help, I am very appreciated. 如果您能提供帮助,我将不胜感激。 Thank you very much. 非常感谢你。
My code: 我的代码:
$(document).ready(function() { var dragging = false; var clickedX, clickedY; // right click event $("#displayWindow").mousedown(function(e) { // when the mouse is pressed, the div is appended to the displayWindow if (e.button == 2) { // append the div start at the location that we click $("#displayWindow").append("<div id='div1'></div>"); // get the coordinate where we clicked and set the div begin with that position clickedX = e.pageX; clickedY = e.pageY; $('#div1').css({ top: clickedY, left: clickedX }); dragging = true; return false; } }); // holding on the mouse button, change the size of the div $("#displayWindow").on("mousemove", function(e) { if (dragging) { var mouseOnX = e.pageX; var mouseOnY = e.pageY; // allow user drag the selection box in 4 different direction $('#div1').css({ top: Math.min(clickedY, mouseOnY), left: Math.min(clickedX, mouseOnX), height: Math.abs(mouseOnY - clickedY), width: Math.abs(mouseOnX - clickedX) }); } }); // end on $(document).on("mouseup", function(e) { dragging = false; }); // when clicked again, the menu fade out, and the div disappear $(document).click(function(e) { if (e.button == 0) { // remove the selection box div $('#div1').remove(); } }); // prevent the default contextmenu on the display window document.getElementById('displayWindow').oncontextmenu = function() { return false; }; }); // end ready
#displayWindow { background-color: white; border: 1px solid; height: 600px; width: 800px; } #div1 { background-color: lightgreen; position: absolute; opacity: 0.3; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <div id="displayWindow"> <svg height="130" width="150" style="position:absolute; left:200; top:200;" class="ui-widget-content"> <text fill="black" x=75 y=75 style="text-anchor: middle">1</text> <path d="M38 0 L113 0 L150 65 L113 130 L38 130 L0 65 Z" / fill="none" stroke="blue"> </svg> </div>
The issue you have is not with the removal. 您遇到的问题与删除无关。 The issues are: 问题是:
mousemove
event, every time you right click. 每次右键单击时, 都将事件处理程序添加到mousemove
事件。 This is not right. 这是不对的。 You need to bind the mousemove
handler only once, so move that code outside of the mousemove
event handler. 您只需要绑定一次mousemove
处理程序,因此将代码移到mousemove
事件处理程序之外。 As a consequence, also declare of clickedX
and clickedY
variables at the ready
function level; 因此,还要在ready
函数级别声明clickedX
和clickedY
变量; e.button
will be 0 always. mousemove事件不提供有关按下按钮的任何信息,因此e.button
始终为0。 Instead you should keep track of whether the mouse button is pressed with a variable, and reset that variable when you detect a mouseup
event on the document. 相反,您应该跟踪是否用变量按下了鼠标按钮,并在检测到文档上的mouseup
事件时重置该变量。 This is not perfect, but probably good enough; 这不是完美的,但可能已经足够了。 Some other notes: 其他注意事项:
div
you add has no style
attribute, so it has no effect to do removeAttr('style')
. 您添加的div
没有style
属性,因此执行removeAttr('style')
无效。 However, the remove()
works; 但是, remove()
起作用; css
styles in one css()
call, by passing an object with key/value pairs. 通过传递带有键/值对的对象,可以在一个css()
调用中更改几种css
样式。 true
from an event handler. 从事件处理程序返回true
没有用。 Math.min
and Math.abs
calls. 您可以结合使用Math.min
和Math.abs
调用来支持4个方向。 Here is some adapted code: 这是一些经过修改的代码:
$(document).ready(function() { var dragging = false; var clickedX, clickedY; // right click event $("#displayWindow").mousedown(function(e) { // when the mouse is pressed, the div is appended to the displayWindow if (e.button == 2) { $('#div1').remove(); // remove div that might still be there. // append the div start at the location that we click $("#displayWindow").append("<div id='div1'></div>"); // get the coordinate where we clicked and set the div begin with that position clickedX = e.pageX; clickedY = e.pageY; $('#div1').css({top: clickedY, left: clickedX}); dragging = true; return false; } }); // holding on the mouse button, change the size of the div $("#displayWindow").on("mousemove", function(e) { if (dragging) { var mouseOnX = e.pageX; var mouseOnY = e.pageY; // allow user to drag the selection box in 4 different directions $('#div1').css({ top: Math.min(clickedY, mouseOnY), left: Math.min(clickedX, mouseOnX), height: Math.abs(mouseOnY - clickedY), width: Math.abs(mouseOnX - clickedX) }); } }); $(document).on("mouseup", function(e) { dragging = false; }); // when clicked the div disappears $(document).click(function(e) { if (e.button == 0) { // remove the selection box div $('#div1').remove(); } }); // prevent the default contextmenu on the display window document.getElementById('displayWindow').oncontextmenu = function() { return false; }; $('#div1').remove(); }); // end ready
#displayWindow { background-color: white; border: 1px solid; height: 600px; width: 800px; } #div1 { background-color: lightgreen; position: absolute; opacity: 0.3; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="displayWindow"> <svg height="130" width="150" style="position:absolute; left:200; top:200;" class="ui-widget-content"> <text fill="black" x=75 y=75 style="text-anchor: middle">1</text> <path d="M38 0 L113 0 L150 65 L113 130 L38 130 L0 65 Z" / fill="none" stroke="blue"> </svg> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.