简体   繁体   中英

How to make a div draggable and contenteditable=“true”?

I have tried over and over but i just can seem to get the text to be editable and also draggable, because when i apply .draggable() the contenteditable="true" becomes "false".

Javascript

$(".note").keyup(function() {
    $(this).css('height' , '100%');
});

HTML

<div class="note" contenteditable="true">
    <span id='close' onclick='this.parentNode.parentNode.removeChild(this.parentNode)'>
        <img src="images/close.png" height="30" width="30" align="right" style="vertical-align: top; float: right"/>
    </span>
</div>

CSS

.note {
    width: 280px;
    height: 130px;
    padding-top: 20px;
    margin-top: 60px;
    margin-left: 25px;
    padding: 2;
    word-break: break-word;
    font-family: Note;
    font-size: 20px;
    background-image: url("images/stickynote.png");
    background-repeat: no-repeat;
    background-size: cover;
    z-index: 1;
}

You can do this to make it so if you double click it will be editable, but single clicks will drag it.

$(".note").draggable()
  .click(function() {
    $(this).draggable( {disabled: false});

}).dblclick(function() {
    $(this).draggable({ disabled: true });
});

Add this to the close buttons <img> code will keep it in the top right of your container:

position: relative;
  top: -20px;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM