12

I'd like to add a jquery color picker fallback if no color picker is shown. For example, chrome shows a color picker, but as of yet, safari simply shows a textfield. Is there any way (without user agents) to detect if a color picker is available?

Edit: Modernizr is no good, since it would just say that safari supports it too. Safari supports input type color because it doesn't allow anything but a #hex color to be entered in the input box. But there is no color picker. I need to know if there is a color picker instead.

4

5 回答 5

5

干得好。

/**
 * Determine if the current browser has support for HTML5 input type of color.
 * @author Matthew Toledo
 * @return {boolean} True if color input type supported. False if not.
 */
var test = function() {
  var colorInput;

  // NOTE:
  //
  // If the browser is capable of displaying a color picker, it will sanitize the color value first. So "!"" becomes #000000;
  //
  // Taken directly from modernizr:
  // @see http://modernizr.com/docs/#features-html5
  //
  // These types can enable native datepickers, colorpickers, URL validation, and so on.
  // If a browser doesn’t support a given type, it will be rendered as a text field. Modernizr
  // cannot detect that date inputs create a datepicker, the color input create a colorpicker,
  // and so on—it will detect that the input values are sanitized based on the spec. In the
  // case of WebKit, we have received confirmation that sanitization will not be added without
  // the UI widgets being in place.
  colorInput = $('<input type="color" value="!" />')[0];
  return colorInput.type === 'color' && colorInput.value !== '!';
};

$(function(){
  if (test()) {
    $('body').append('<p1>Your browser supports the color input</p>');
  } else {
    $('body').append('<p>Your browser Doesn\'t Support the color input</p>');
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

于 2015-10-12T20:36:23.537 回答
3

要检查任何浏览器上对 HTML3 或 CSS3 的任何功能的支持,您可以使用modernizr

颜色选择器的代码将是:

if(!Modernizr.inputtypes.color){
  // your fall back goes here
}

对于modernizr,您所要做的就是在您的网页上添加一个modernizr 的链接。

您可以在 nettuts 上查看相同的运行演示:

http://net.tutsplus.com/tutorials/html-css-techniques/how-to-build-cross-browser-html5-forms/

希望这会帮助你。

谢谢, NS

于 2012-12-09T16:31:57.493 回答
3

再次没有 jQuery

const hasColorInputSupport = (document) => {
    try {
        const input = document.createElement('input');
        input.type = 'color';
        input.value = '!';
        return input.type === 'color' && input.value !== '!';
    } catch (e) {
        return false;
    };
};
于 2018-07-04T07:42:50.253 回答
1

您可以使用 Modernizr.js 检测 HMTL5 功能并添加回退。

这是有关使用 Modernizr 的简短介绍。

http://html5doctor.com/using-modernizr-to-detect-html5-features-and-provide-fallbacks/

于 2012-12-09T16:16:58.807 回答
1

带有颜色池的颜色输入不能选择其不存在的文本 - 因此它们没有 selectionStart 或 selectionEnd 属性。

我发现这是一个比 Modernizr 检查更有用的检查,Modernizr 检查只检查控件不能包含除十六进制代码以外的其他内容。

在 Safari 12、Chrome 69、Firefox 62、Internet Explorer 11 和 Edge 17 中测试。

编辑:此方法不支持 Safari 12.1 对颜色选择器的支持。欢迎任何建议的修复。

var supportsColor = true;

try {
  var a = document.createElement("input");
  a.type = "color";
  supportsColor = a.type === "color" && typeof a.selectionStart !== "number";
} catch (e) {
  supportsColor = false;
}

document.getElementsByTagName("output")[0].innerText = supportsColor.toString();
Supports color input with color picker: <output></output>

<input type="color"/>

于 2018-09-20T11:20:21.737 回答