简体   繁体   English

如何通过javascript将元素置于contenteditable模式

[英]How do I put an element into contenteditable mode via javascript

I have the following HTML element 我有以下HTML元素

<p class="story" contenteditable="true">This is a story.</p>

Now I want to put the element into edit mode via javascript. 现在我想通过javascript将元素置于编辑模式。 I assumed something like the following would work, but it doesn't 我假设以下内容可行,但事实并非如此

$('.story').click();

How can I manually put this element in edit mode (via javascript or any other way) without the user clicking on the element? 如何在没有用户点击元素的情况下手动将此元素置于编辑模式(通过javascript或任何其他方式)?

使用focus而不是click

$(".story").focus();
$('.story').focus(function(){
    $(this).attr('contenteditable', 'true');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过JavaScript检测对contentEditable的支持? - How do I detect support for contentEditable via JavaScript? 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? 如何将大量文本发送到 contenteditable=&quot;true&quot; 元素中? - How do I send a large amount of text into a contenteditable="true" element? 如何使用 JavaScript 检测图像是否已粘贴或拖放到内容可编辑区域? - How do I detect, with JavaScript, if an image was pasted or dropped into a contenteditable area? 如何通过JavaScript取消隐藏隐藏的asp元素? - How do I unhide a hidden asp element via JavaScript? 如何在contenteditable中将元素切换为execCommand? - Javascript - How to toggle an element in contenteditable as execCommand? 我如何从php提取一个值,以通过javascript将其放入输入元素? - How can I pick up a value from php to put it into input-element via javascript? 如果知道元素ID,如何在contenteditable div中选择(获取范围或选择对象)元素? - How do I select (get range or selection object) an element in a contenteditable div if I know the element ID? 如何在 contenteditable 中转义格式化元素? - How do you escape a formatted element in a contenteditable? 如何使用简单的JavaScript / CSS将元素放在DOM中任何给定元素的上方? - How do I put an element above any given element in the DOM with simple JavaScript/CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM