简体   繁体   English

如何删除所有div标签,而不删除其内容? (使用contenteditable =“ true”,HTML5)

[英]How to remove all div tags, but not content, which it have? (Working with contenteditable=“true”, HTML5)

How to remove all div tags, but not content, which it have? 如何删除所有div标签,而不删除其内容? (Working with conteneditable="true", HTML5) (使用conteneditable =“ true”,HTML5)

I have a not real textarea - just <div> with contenteditable="true" . 我有一个不是真正的textarea- <div>具有contenteditable="true" When I'm pressing 'submit' button, code is reading innerHTML of this element. 当我按下“提交”按钮时,代码正在读取此元素的innerHTML。 It's not good. 这不好。

So, I want to use it as textarea, I need to: 因此,我想将其用作文本区域,我需要:

  1. Remove <div> tags without content (when I'm starting new line, browser closing previous <div> (if exist) and starting new <div> . 删除没有内容的<div>标记(当我开始换行时,浏览器将关闭先前的<div> (如果存在)并启动新的<div>
  2. Remove all other tags, different from <br /> (but if <br> tags more than 2 on a row, delete it). 删除所有与<br />不同的其他标签(但如果<br>连续超过2个标签,则将其删除)。

How to realize it? 如何实现呢? On server side and on user side (because it sends by xhr). 在服务器端和用户端(因为它是通过xhr发送的)。

get your parent <div> , clone the node, and traverse the cloned Tree: remove the <div> Nodes, which have no content, and the other disallowed tags. 获取父<div> ,克隆节点,并遍历克隆的树:删除没有内容的<div>节点,以及其他不允许的标签。

When you finished Traversing, you the can submit what is left of your Dom-Element. 完成遍历后,您可以提交Dom元素剩余的内容。

I have used content editable divs in the past, the best way to use them is indirectly; 我过去曾经使用过内容可编辑的div,最好的使用方法是间接使用它们。 rather than having them throw the content at php, have a hidden textarea somewhere, with the submit button in that (the submit not hidden obviously) and then the content editable div elsewhere. 与其让他们将内容扔到php上,不如在某个地方有一个隐藏的textarea ,并在其中具有“提交”按钮(“提交”显然没有被隐藏),然后在其他地方提供可编辑div的内容。 You can then use jQuery to grab the content of your div and then use some funky regex to parse it for your <br /> and <div> and </div> tags. 然后,您可以使用jQuery来获取div的内容,然后使用一些时髦的正则表达式将其解析为您的<br /><div></div>标记。

<div contenteditable="true" style="width:200px; height:100px;" id="InputDiv">
...
</div>

<form action="something.php">
    <textarea id="InputArea" style="visibility:hidden;">
    <input onclick="getData()"type="submit" value="submit" >
</form>

With the corresponding jQuery: 与相应的jQuery:

function getData(){
    var rawDiv = $("#InputArea").text();
    var parsedStr = /*some funky regex*/;
    $("#InputArea").text(parsedStr);
}

I seem to have misplaced my previous work with jquery and contenteditable divs, but something along this line should work. 我似乎将我以前的工作与jQuery和contenteditable div放在一起了,但是应该沿这条线工作。

An alternative: 替代:

Rather than using content editable divs, why not use markdown? 为什么不使用markdown而不是使用内容可编辑的div? There would be less need to sort out this kind of problem. 解决此类问题的需求将减少。

这可以解决问题:

document.getElementById(/*your_div_to_be_removed*/).removeNode(false);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM