简体   繁体   English

contenteditable 不能在 safari 中工作,但可以在 chrome 中工作

[英]contenteditable not working in safari but works in chrome

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..这在 chrome 中按预期工作,但在 safari 中它只会......发光但不会对键输入做出反应..

this is the method that fires the text edition:这是触发文本版本的方法:

var namebloc = $(event.currentTarget).find('.column_filename');
var oldvalue = namebloc.html();

namebloc.attr('contentEditable', true).focus();
document.execCommand('selectAll',false,null);

namebloc.blur(function() 
    {
    $(this).attr('contentEditable', false).unbind( "keydown" ).unbind( "blur" );
    var newvalue = $(this).html().replace('"','&quot;').replace(/(<([^>]+)>)/ig,"");
    console.log(newvalue);
    });
namebloc.keydown(function(e)
    {
    if(e.keyCode==27){ $(this).html(oldvalue);}//escape
    if(e.keyCode==13){  $(this).blur(); }//enter    
    });

This is a screenshot in chrome when fired this works as expected...这是 chrome 中的屏幕截图,按预期工作...在此处输入图片说明

and this is the result in safari.. no reaction to keyboard or mouse selection:这是在 safari 中的结果……对键盘或鼠标选择没有反应:在此处输入图片说明

Any idea why and how to solve this in safari?知道为什么以及如何在 safari 中解决这个问题吗?

this is the HTML before the method is called :这是调用方法之前的 HTML:

<span field="filename" class="column_filename" style="width:763px;">eiffel (2).JPG</span>

This is when it's called (at the same time as screenshots)这是它被调用的时间(与屏幕截图同时)

<span field="filename" class="column_filename" style="width:763px;" contenteditable="true">eiffel (2).JPG</span>

Safari has the user-select CSS setting as none by default.默认情况下,Safari 的user-select CSS 设置为none You can use:您可以使用:

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

To make it work.让它发挥作用。

In Safari, despite also being WebKit based, there is differing behavior when clicking on an element that has the user-select property set.在 Safari 中,尽管也基于 WebKit,但单击具有user-select属性集的元素时会出现不同的行为。 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. Chrome 似乎关闭了该 css 属性并阻止任何焦点转到被单击的元素(感谢您继续开发现代且明智的浏览器 Google),而 Safari 对该 css 属性没有做任何关于焦点的事情,而是将焦点设置在无论如何点击元素。

One solution in Safari is to use a proper html button element, but this sucks from a styling perspective. Safari 中的一种解决方案是使用适当的 html 按钮元素,但从样式的角度来看,这很糟糕。

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.更好的解决方案是捕获 mousedown 事件(单击时第一个触发 Mouse 类型事件),并在该事件上preventDefault() This should work in any version of Safari.这应该适用于任何版本的 Safari。

For example例如

<span onclick="document.execCommand('bold', false);" onmousedown="event.preventDefault();">
  <i class="material-icons">format_bold</i>
</span>

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

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