javascript - How to delete whole span element with contenteditable spans inside it? - Stack Overflow

I have a contenteditable div. Inside, I have put a 'fraction' span, with regards to this link

I have a contenteditable div. Inside, I have put a 'fraction' span, with regards to this link. Here's my current code (the numbers in the spans are just dummy numbers):

<span style="display:inline-block; vertical-align:middle;" contenteditable="false">
  <span style="display:block; min-width: 20px;border-bottom:1px solid black; text-align: center; outline: none;" contenteditable>6</span>
  <span style="display:block; min-width: 20px;text-align: center;outline: none;" contenteditable>7</span>
</span>

My problem is, when I try to delete this 'fraction' span (the outer span, and it should be deletable, as this is a Maths WYSIWYG Program), it just removes the content of the two inner contenteditable spans. When I try to delete it (obviously from outside the outer span but inside the contenteditable div), I want all three elements to get removed. I don't want the outer span still lingering.

Any solutions?

UPDATE: If there is no way to do this, is there a way to select that whole span element and its contents when the user presses the delete key next to the big fraction span?

I have a contenteditable div. Inside, I have put a 'fraction' span, with regards to this link. Here's my current code (the numbers in the spans are just dummy numbers):

<span style="display:inline-block; vertical-align:middle;" contenteditable="false">
  <span style="display:block; min-width: 20px;border-bottom:1px solid black; text-align: center; outline: none;" contenteditable>6</span>
  <span style="display:block; min-width: 20px;text-align: center;outline: none;" contenteditable>7</span>
</span>

My problem is, when I try to delete this 'fraction' span (the outer span, and it should be deletable, as this is a Maths WYSIWYG Program), it just removes the content of the two inner contenteditable spans. When I try to delete it (obviously from outside the outer span but inside the contenteditable div), I want all three elements to get removed. I don't want the outer span still lingering.

Any solutions?

UPDATE: If there is no way to do this, is there a way to select that whole span element and its contents when the user presses the delete key next to the big fraction span?

Share Improve this question edited May 23, 2017 at 10:34 CommunityBot 11 silver badge asked Aug 29, 2012 at 7:06 ZeriumZerium 17.4k32 gold badges118 silver badges187 bronze badges 2
  • Could you show what you tried? Or at least a bit of js to better understand your issue? – Gil Zumbrunnen Commented Aug 29, 2012 at 7:22
  • No it's actually simple enough - I'm trying to create a Maths WYSIWYG Program as mentioned above, and I'm stuck on the part where after a user creates a 'fraction', they have difficulty deleting it. – Zerium Commented Aug 29, 2012 at 7:25
Add a ment  | 

1 Answer 1

Reset to default 3

Just provide the top span with an id:

<span id="editfraction" 
      style="display:inline-block; vertical-align:middle;" 
      contenteditable="false">

and use this to delete it:

$('#editfraction').remove();

If you want to delete the span when the user presses the delete button, use ids, bined with a data attribute on the contentEditable spans.

<span ... data-ondeleteId="#editx" contenteditable>6</span>

See this jsfiddle for an example

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745533693a4631809.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信