简体   繁体   English

jQuery即使在直接加载文档时也不会触发点击?

[英]jQuery not firing click even when directed to on document load?

I am wringing a color picker since I couldn't find one with the feature I want (you'd think by now all possible features would have been included, but I couldn't find one that supported RGBA output with an adjustable alpha level) and I'm having issues with the part of the script that will load the current color when the editor is started. 我正在绞合一个拾色器,因为我找不到想要的功能(您可能认为现在所有可能的功能都已包括在内,但是我找不到能够支持具有可调Alpha级别的RGBA输出的功能)我在启动编辑器时加载当前颜色的脚本部分遇到了问题。 I won't bore you with all the code, I'll try to keep it to the basics. 我不会为您带来所有代码,我会尽力使其保持基础。

First, all of this is inside a standard $(docuemnt).ready(), the fist thing it does is replace inputs with my color selector code, then it tries to run the set_color function. 首先,所有这些都在标准的$(docuemnt).ready()内部,它要做的第一件事是用我的颜色选择器代码替换输入,然后尝试运行set_color函数。

    $('.rgbapicker').each(function(){
        $(this).replaceWith(
            'This is all the display code, it's working fine.'
        )
        set_color('#'+$(this).attr('name')+' ',$(this).val())
    });

The set_color function takes the selected variable, which controls which color picker your using and the color, which is converted using tinycolor.js - a modified version that passes rgb alpha through the toRgb() function (as opposed to only passing it in the .toRgbString() function). set_color函数采用选定的变量,该变量控制您使用的颜色选择器和颜色,该颜色使用tinycolor.js进行转换-修改后的版本将rgb alpha通过toRgb()函数传递(而不是仅将其传递给。 toRgbString()函数)。

    function set_color(selected,color){
        var color = tinycolor(color).toRgb();
        $(selected+'.disp_s_color_r').val(color.r); // This code
        $(selected+'.disp_s_color_g').val(color.g); // Changes the
        $(selected+'.disp_s_color_b').val(color.b); // Color selector
        $(selected+'.disp_s_color_a').val(color.a); // Display
        var color = tinycolor(color).toHsl();
        console.log(selected+' - '+color.h+' - '+color.s+' - '+color.l);
        var e = new jQuery.Event("click");
        e.pageX = 180;
        e.pageY = 180;
        $('.colorbase').trigger(e);

The editor uses a hsl spectrum image that you click on, which then gets the mouse coordinated and does the math to figure it all out, it's a very basic method and works fine. 编辑器使用您单击的hsl光谱图像,然后协调鼠标并进行数学计算以将其全部识别出来,这是一种非常基本的方法,效果很好。 When I click on the editor it updates properly (that doesn't use anything writen here), when I hit my reset button it fires of this function and works correctly (the XY of the selection area it updates, highlighting the color you selected) but when this is loaded in that initial loop it doesn't fire. 当我单击编辑器时,它会正确更新(不使用此处写的任何内容),当我按下“重置”按钮时,它将触发此功能并正常工作(它会更新选择区域的XY,突出显示您选择的颜色)但是在初始循环中加载它时不会触发。 The set_color function runs, and updates the in editor display, but the selector doesn't move, and since that click function is what updates all the previews and the color that is written to the (not hidden) input none of that is updated. set_color函数将运行,并更新in编辑器中的显示,但选择器不会移动,并且由于单击函数可更新所有预览,而写入(未隐藏)输入的颜色则不会更新。

There is no difference between the initial post-replace loop call and the reset buttons call, I even checked the events and there exact copies of each other (I copied them into another program and ran a compare, they are exact copies). 初始替换后循环调用和重置按钮调用之间没有区别,我什至检查了事件,并且彼此之间有精确的副本(我将它们复制到另一个程序中并运行比较,它们是精确的副本)。 I've tried the ideals posted in other similar messages here (and elsewhere when googling the issue), including having the loop simulate the click on the reset button, but nothing has worked. 我已经尝试过在此处(以及在搜索该问题时的其他地方)其他类似消息中发布的理想,包括让循环模拟对重置按钮的单击,但是没有任何效果。

Any ideas? 有任何想法吗? I'd love to just get the click to properly fire, otherwise it'll be a chuck of code to simulate a click that will do nothing but make the script bigger (it's not exactly trim right now). 我只想让点击正确触发,否则将是一堆代码,无法模拟点击,只会使脚本变大(现在还不完全修剪)。

You have a typo; 你有错字;

    $(this).replaceWith(
        "This is all the display code, it's working fine." // all singlequotes before
    )

Since e is a window / document Event, should you not fire it on the document instead on the color picker? 由于e是窗口/文档事件,因此您是否应该在文档上而不是在颜色选择器上触发它?

As seen on jQuery API there are two mistakes: 如jQuery API所示,有两个错误:

  • don't create the event with the "new" keyword 不要使用“ new”关键字创建事件
  • Trigger event on the body element 在body元素上触发事件

尝试转义replaceWith字符串中的撇号。

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

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