【发布时间】:2017-06-15 12:50:59
【问题描述】:
我有一个数组。我在其中存储了标签列表。现在我想要的是,点击像(h1,a,strong)这样的元素,它将在给定数组中找到标签并在元素上应用 contenteditable true 。在标签或其他元素外部单击时,contenteditable 将再次设置为 false。
目前我正在使用此代码来更改 contenteditable true 但如果我想编辑 p 我必须为 p 编写一个与强 h2、h3、h4 等相同的新代码
$('h1').click(function(event) {
$(this).attr('contenteditable', 'true');
});
$(function(){
$(".textTemplate").draggable({
helper: "clone",
zIndex: 2500
});
$( ".editorDesignView" ).droppable({
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) {
event.preventDefault();
$(this).attr('contenteditable', 'true');
});
});
* {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 好的。但请帮助我
-
当我无法重现您的问题时,我该如何帮助您
-
如果您想将事件附加到多个元素,那么您可以对所有元素使用
$('h1,h2,h3,h4,p,strong')或$('*')! -
@PrashantShirke 但是,如果我不希望它应用于 div 部分怎么办
标签: javascript jquery html css jquery-ui