jquery 颜色选择器 Spectrum 在 Internet Explorer 上不工作
jquery color picker Spectrum not working on Internet explorer
我正在使用 Spectrum 作为颜色选择器的 jquery 插件,以便在 contenteditable div 中使用它。在 chrome 和 firefox 中它运行完美。但是在 Internet Explorer 中它只显示调色板,但是当我 select 什么都没有改变。
但是,如果我以这种方式直接执行 execCommand
它是有效的:
$('#btn-color_font').click(function() {
document.execCommand('foreColor', false, "#ff0000");
});
我做错了什么?请帮助我如何在 IE 中使用它。谢谢。
片段html:
<li class="main-btn">
<a href="#" id="btn-color_font" class="wysiwyg-color-spectrum-cF">cF</a>
</li>
<li class="main-btn" >
<a href="#" id="btn-color_background" class="wysiwyg-color-spectrum-bF">cB</a>
</li>
片段 js:
$(".wysiwyg-color-spectrum-cF").spectrum({
showPaletteOnly: true,
togglePaletteOnly: true,
togglePaletteMoreText: 'more',
togglePaletteLessText: 'less',
color: 'blanchedalmond',
change: function (color) {
document.execCommand('foreColor', false, color.toHexString());
},
hideAfterPaletteSelect: true,
palette: [
["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
]
});
您需要为 IE 添加专有滤镜以获得渐变支持
结帐:http://bgrins.github.io/spectrum/#details-ieImplementation
这里的问题是在您单击颜色之前 IE 会丢失 focus/selection,这就是它无法正常工作的原因。它会触发 change
事件,但由于没有选择任何内容,因此什么也不会发生。
作为解决方法,您必须在单击 Spectrum 按钮时保存选择,然后在触发 Spectrum 的 change
事件时恢复该选择。
类似的东西:
var WinSelection = (function(w, d) {
var currentSelection = null; // create a variable to save the current selection
function saveSelection() { // saveSelection copied from another SO answer
if (w.getSelection) {
sel = w.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (d.selection && d.selection.createRange) {
return d.selection.createRange();
}
return null;
}
function restoreSelection(range) { // restoreSelection copied from another SO answer
if (range) {
if (w.getSelection) {
sel = w.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (d.selection && range.select) {
range.select();
}
}
}
return { // return an object with two public methods: saveSelection and restoreSelection
saveSelection: function() {
currentSelection = saveSelection();
},
restoreSelection: function() {
restoreSelection(currentSelection);
}
};
})(window, document);
然后,在您的代码中,您可以在单击按钮时保存选择:
$('#wysiwyg-editor li a').click(function() {
WinSelection.saveSelection();
});
在你的 change
事件中,你恢复选择:
/* ... */
change: function (color) {
WinSelection.restoreSelection();
document.execCommand('foreColor', false, color.toHexString());
},
/* ... */
change: function (color) {
WinSelection.restoreSelection();
document.execCommand("BackColor", false, color.toHexString());
},
/* ... */
这里是 your fiddle - 已更新,可在 IE 中使用。
想要提供另一个对我有用的解决方案:
将频谱插件生成的这三个div添加html属性unselectable='on'
(我是在站点加载完成后执行代码):
jQuery(".sp-replacer").attr("unselectable", "on");
jQuery(".sp-preview").attr("unselectable", "on");
jQuery(".sp-preview-inner").attr("unselectable", "on");
这可以防止失去对 selected 文本的关注。根据我的测试,这应该不会影响其他浏览器中的正常行为(在 Firefox、Chrome 和 Opera 上测试)。
编辑:简单颜色 selection 有效,但如果我想 select 预定义颜色或在显示的输入字段中手动输入另一个值,selection 仍然会丢失。
我正在使用 Spectrum 作为颜色选择器的 jquery 插件,以便在 contenteditable div 中使用它。在 chrome 和 firefox 中它运行完美。但是在 Internet Explorer 中它只显示调色板,但是当我 select 什么都没有改变。
但是,如果我以这种方式直接执行 execCommand
它是有效的:
$('#btn-color_font').click(function() {
document.execCommand('foreColor', false, "#ff0000");
});
我做错了什么?请帮助我如何在 IE 中使用它。谢谢。
片段html:
<li class="main-btn">
<a href="#" id="btn-color_font" class="wysiwyg-color-spectrum-cF">cF</a>
</li>
<li class="main-btn" >
<a href="#" id="btn-color_background" class="wysiwyg-color-spectrum-bF">cB</a>
</li>
片段 js:
$(".wysiwyg-color-spectrum-cF").spectrum({
showPaletteOnly: true,
togglePaletteOnly: true,
togglePaletteMoreText: 'more',
togglePaletteLessText: 'less',
color: 'blanchedalmond',
change: function (color) {
document.execCommand('foreColor', false, color.toHexString());
},
hideAfterPaletteSelect: true,
palette: [
["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
]
});
您需要为 IE 添加专有滤镜以获得渐变支持
结帐:http://bgrins.github.io/spectrum/#details-ieImplementation
这里的问题是在您单击颜色之前 IE 会丢失 focus/selection,这就是它无法正常工作的原因。它会触发 change
事件,但由于没有选择任何内容,因此什么也不会发生。
作为解决方法,您必须在单击 Spectrum 按钮时保存选择,然后在触发 Spectrum 的 change
事件时恢复该选择。
类似的东西:
var WinSelection = (function(w, d) {
var currentSelection = null; // create a variable to save the current selection
function saveSelection() { // saveSelection copied from another SO answer
if (w.getSelection) {
sel = w.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (d.selection && d.selection.createRange) {
return d.selection.createRange();
}
return null;
}
function restoreSelection(range) { // restoreSelection copied from another SO answer
if (range) {
if (w.getSelection) {
sel = w.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (d.selection && range.select) {
range.select();
}
}
}
return { // return an object with two public methods: saveSelection and restoreSelection
saveSelection: function() {
currentSelection = saveSelection();
},
restoreSelection: function() {
restoreSelection(currentSelection);
}
};
})(window, document);
然后,在您的代码中,您可以在单击按钮时保存选择:
$('#wysiwyg-editor li a').click(function() {
WinSelection.saveSelection();
});
在你的 change
事件中,你恢复选择:
/* ... */
change: function (color) {
WinSelection.restoreSelection();
document.execCommand('foreColor', false, color.toHexString());
},
/* ... */
change: function (color) {
WinSelection.restoreSelection();
document.execCommand("BackColor", false, color.toHexString());
},
/* ... */
这里是 your fiddle - 已更新,可在 IE 中使用。
想要提供另一个对我有用的解决方案:
将频谱插件生成的这三个div添加html属性unselectable='on'
(我是在站点加载完成后执行代码):
jQuery(".sp-replacer").attr("unselectable", "on");
jQuery(".sp-preview").attr("unselectable", "on");
jQuery(".sp-preview-inner").attr("unselectable", "on");
这可以防止失去对 selected 文本的关注。根据我的测试,这应该不会影响其他浏览器中的正常行为(在 Firefox、Chrome 和 Opera 上测试)。 编辑:简单颜色 selection 有效,但如果我想 select 预定义颜色或在显示的输入字段中手动输入另一个值,selection 仍然会丢失。