我有一个数组。我在其中列出了标签清单。现在我想要的是,点击像(h1,a,strong)这样的元素,它会在给定数组中找到标签,并在元素上应用contenteditable true。在标签或其他元素外部点击时,contenteditable将再次设置为false。如何在单击元素时设置contenteditable true
目前我使用这个代码改变CONTENTEDITABLE真实的,但如果我想编辑p I将不得不写的p一样强大的H2,H3,H4等新的代码
$('h1').click(function(event) {
$(this).attr('contenteditable', 'true');
});
$(function(){
$(".textTemplate").draggable({
helper: "clone",
zIndex: 2500
});
$(".editorDesignView").droppable({
\t \t accept: '.textTemplate',
drop: function(event, ui) {
var html = `<div id="" class="dynamic"><h1 contenteditable="false">Title</h1><p contenteditable="false" style="padding: 5px;">Add your text here.</p></div>`;
$(html).appendTo(this).hide().slideDown();
}
});
$(document).on('click', 'h1', function(event) {
\t \t event.preventDefault();
\t \t $(this).attr('contenteditable', 'true');
\t });
});
* {box-sizing: border-box;}
#wrapper {width: 100%; height: 100vh;}
.templateWrapper {width: 30%; height: 100%;float:left;overflow-y: scroll;}
.editorBlock {width: 70%; height: 100%;float:left;position: relative;background-color:#f1f1f1}
.editorDesignView {width: 100%; height: 100%;}
.dynamic {
background: #4073ff;width: 80%; margin: 10px auto; padding: 10px;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div id="wrapper">
<div class="templateWrapper">
<div class="textTemplate" style="margin-top:20px;" class="template">
<div>drag me</div>
</div>
</div>
<div class="editorBlock" style="height:100%;">
<div class="editorDesignView" style="height:100%;"></div>
</div>
</div>
您的代码对我而言 –
@CarstenLøvboAndersen好的。但请帮助我 – user7791702
我无法重现您的问题时我能如何帮助您 –