[英]How can I set the contenteditable true on clicking the element
I have an array. 我有一个数组。 In which I have strored the list of tags. 我在其中存储了标签列表。 Now what I want, that on click on the element like ( h1, a, strong ) it will find the tags in the given array and apply contenteditable true on the element. 现在我想要的是,单击元素(h1,a,strong)时,它将在给定数组中找到标签,并将contenteditable true应用于该元素。 On clicking outside the tag or other element the contenteditable will again set to false. 在标签或其他元素之外单击时,contenteditable将再次设置为false。
Currently I am using this code for changing the contenteditable true but if I want to edit p I have to write a new code for p same as strong h2, h3, h4 etc 当前,我正在使用此代码来更改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>
Solved your issue please use this code it will help you better 解决了您的问题,请使用此代码,它将帮助您更好
$(document).on('click', '*', function(event) {
event.preventDefault();
$(this).attr('contenteditable', 'true');
});
In your fiddle, you are missing a bit of jquery for the p element. 在您的小提琴中,您缺少用于p元素的jquery。
$(function(){ $(".textTemplate").draggable({ helper: "clone", zIndex: 2500 }); $( ".editorDesignView" ).droppable({ accept: '.textTemplate', drop: function( event, ui ) { var html = '<div id="" style="background: #4073ff;width: 80%; margin: 10px auto; padding: 10px;"><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'); }); $(document).on('click', 'p', 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%;}
<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>
You can do that with following code. 您可以使用以下代码来实现。
Attach event to all elements except html,body and div to make contenteditable and for other elements attach event to make non content editable. 将事件附加到html,body和div以外的所有元素以使内容可编辑,而其他元素将事件附加使非内容可编辑。
As per your requirement you can add selectors in $array
comma seperated. 根据您的要求,您可以在$array
逗号分隔添加选择器。
var $array=[ 'body', 'div', 'html' ];
var notSelectorString = $array.join(",");
$('*').not(notSelectorString).on('click', function(event) {
$(this).attr('contenteditable', 'true');
}).on("focusout",function(e){ $(this).attr('contenteditable', 'false');});
$(notSelectorString).on('click', function(event) {
$(this).attr('contenteditable', 'false');
});
您可以使用一个名为eval()
的相当危险的js函数,该eval()
可以从字符串执行代码,这样您就可以将其置于for
循环中,并每次在h
之后更改整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.