最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论