Insert link in contenteditable element

前端 未结 5 1666
灰色年华
灰色年华 2020-12-04 11:26

I\'m working on a simple blog system and I\'m using contenteditable so that users can format the text.

Up to now everything works like a charm.

Next thing i

5条回答
  •  伪装坚强ぢ
    2020-12-04 12:26

    I would do it this way:

    1. Create a link with a (possibly unique) initial bogus href attribute to identify it by.
    2. Fetch that element using document.querySelector('a[href=]').
    3. You now have a reference to the created element and can do with it as you please.

    The benefit of this is that you don't have to work with Selection at all.

提交回复
热议问题