使用JQuery触发单击HTML 5颜色选择器 [英] Trigger click on HTML 5 color picker with JQuery

查看:172
本文介绍了使用JQuery触发单击HTML 5颜色选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JQuery中,我们能够触发任何给定元素的点击:

Within JQuery we are able to trigger 'click' on any given element with:

$(selector).trigger('click');

虽然当元素是HTML 5颜色选择器和'display'属性时我遇到了困难由CSS设置为'none'。

Though i have difficulties doing so when the element is a HTML 5 color picker and the 'display' property is set to 'none' by CSS.

通常我们可以在输入类型为'file'或...的情况下执行此操作。

Normally we can do this if the input type is 'file' or ...

那么无论如何要用JQuery或普通的javascript来完成这个工作吗?

So is there anyway to get this done with JQuery or plain javascript?

HTML元素

input type="color" id="fontSel" style="display: none;"
div id="fontWrap"

JQuery

$("#fontWrap").click(function(){
     $("#fontSel").trigger("click");
});




ANSWER

将样式更改为:

input type="color" id="fontSel" style="position:absolute; left:-9999px; top:-9999px;"


推荐答案

这需要用户互动,这意味着你无法触发它用户点击文档中的某个位置:

This need user interaction, meaning you cannot trigger it without user clicking somewhere in document:

DEMO

$("#fontWrap").click(function () {
    $("#fontSel")[0].click();
});

这篇关于使用JQuery触发单击HTML 5颜色选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆