contenteditable not working in safari but works in chrome

后端 未结 2 837
囚心锁ツ
囚心锁ツ 2021-02-19 00:28

I\'m having a strange issue...

This works in chrome as expected but in safari it only gets .. glowing but doesn\'t react on key input..

this is the method that f

相关标签:
2条回答
  • 2021-02-19 00:42

    In Safari, despite also being WebKit based, there is differing behavior when clicking on an element that has the user-select property set. Chrome seems to key-off that css property and prevent any focus going to the element that was clicked (thanks for continuing to develop a modern and sensible browser Google), while Safari does nothing with that css property regarding focus, and sets focus on the clicked element anyway.

    One solution in Safari is to use a proper html button element, but this sucks from a styling perspective.

    A better solution is to trap the mousedown event (the first to fire of the Mouse type events in a click), and preventDefault() on the event. This should work in any version of Safari.

    For example

    <span onclick="document.execCommand('bold', false);" onmousedown="event.preventDefault();">
      <i class="material-icons">format_bold</i>
    </span>
    
    0 讨论(0)
  • 2021-02-19 00:44

    Safari has the user-select CSS setting as none by default. You can use:

    [contenteditable] {
        -webkit-user-select: text;
        user-select: text;
    }
    

    To make it work.

    0 讨论(0)
提交回复
热议问题