简体   繁体   English

如何在单击元素时将contenteditable设置为true

[英]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.

相关问题 即使需要设置webView contentEditable = true,如何阻止键盘出现? - How can I block the keyboard from appearing even though I need to set my webView contentEditable=true? 如何使用Selenium和python将字符序列发送到contenteditable =“ true”元素 - How can i send character sequence to a contenteditable=“true” element by using selenium and python 如何将大量文本发送到 contenteditable=&quot;true&quot; 元素中? - How do I send a large amount of text into a contenteditable="true" element? 如何模糊contentEditable = true的div? - How can I blur a div where contentEditable=true? 我如何在将contenteditable设置为true的父div中设置光标,但是我有 - how do i set cursor in the parent div which is contenteditable set to true but i have 如何知道用户是否删除了 contenteditable div 中的元素? - How can I know if an user deleted an element in a contenteditable div? 如何获得一个可疑的html元素的光标行号? - How can i get the cursor line number of a contenteditable html element? 如何以编程方式模拟输入内容可编辑的 HTML 元素? - How can I programmatically simulate typing in a contenteditable HTML element? 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? 如何在contentEditable元素的子元素中设置光标? - How to set the cursor in the child of a contentEditable element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM