简体   繁体   English

跨两个脚本共享事件或使用更改将可见性状态作为事件触发器

[英]Sharing an event across two scripts or using change is visibility state as an event trigger

OK I am lost here. 好吧,我在这里迷路了。 I have read numerous postings here and else where on the topic of how to check for the state of a given element in particular whether it is visible or hidden and make the change of state trigger an event. 我在这里和其他地方都读过许多文章,其中涉及如何检查给定元素的状态的主题,尤其是它是可见的还是隐藏的,以及使状态改变触发事件。 But I cannot make any of the suggested solutions work. 但是我无法使任何建议的解决方案起作用。

Firstly, as every one seems to leap on this point first, the need to do this has arisen as I have one jQuery script which deals with displaying an svg icon in a clickable state. 首先,由于每个人似乎首先都跳上了这一点,因此,由于我有一个jQuery脚本可处理以可点击状态显示svg图标,因此需要进行此操作。 And another which already has functions to perform relevant actions when the form is made visible by clicking the icon and obviously I want to reuse these. 还有一个已经具有通过单击图标使表单可见时执行相关操作的功能,显然我想重复使用这些功能。

What I have tried: 我尝试过的

Initially I tried have both the scripts acting on a single click event (this remains the ideal solution).... 最初,我尝试将两个脚本都作用于单击事件(这仍然是理想的解决方案)。

Script 1: 脚本1:

 $(".booked").on("click", function() {
         $("#cancellation_Form_Wrapper").css("visibility","visible");
 }).svg({loadURL: '../_public/_icons/booked.svg'});


Script 2:

 $(".booked").on("click", function() {
 // do stuff
 });

This did not work so I tried to research sharing an event across two scripts but couldn't make any head way on this subject so I tried triggering another event for the second script to pick up.... 这没有用,所以我试图研究在两个脚本之间共享一个事件,但是无法在这个主题上取得任何进展,所以我尝试触发另一个事件以使第二个脚本开始工作。

Script 1: 脚本1:

 $(".booked").on("click", function() {
         $("#cancellation_Form_Wrapper").css("visibility","visible");
         $("#cancellation_Form_Wrapper").trigger("change");
 }).svg({loadURL: '../_public/_icons/booked.svg'});

Script 2 剧本2

 $("#cancellation_Form_Wrapper").on("change", function(event){
    // do stuff
 });

This did not work again I am unclear why this didn't have the desired effect. 这没有再次起作用,我不清楚为什么它没有达到预期的效果。

Then I tried is(:visible) .... 然后我尝试了is(:visible)....

Script 1 脚本1

 $(".booked").on("click", function() {
         $("#cancellation_Form_Wrapper").css("visibility","visible");
 }).svg({loadURL: '../_public/_icons/booked.svg'});

Script 2 剧本2

 $("#cancellation_Form_Wrapper").is(":visible", function(){
 // do stuff
 });

So I am a bit lost. 所以我有点迷路。 The ideal would be to return to the first notion. 理想的情况是回到第一个概念。 I do not understand why the click event on the svg cannot be handled by both scripts. 我不明白为什么两个脚本都无法处理svg上的click事件。 I assume that this has something to do with event handlers but I am not sure how I could modify the script so they both picked up the same event. 我认为这与事件处理程序有关,但是我不确定如何修改脚本,以便它们都拾取相同的事件。

Failing that I could use the fact the visibility state changes to trigger the action. 无法使用可见性状态更改这一事实来触发操作。

Any guidance welcomed. 欢迎任何指导。

Edit Ok I have just resolved the issue with script 2 picking up the triggered event from script 1. Sad to say this was a basic error on my part ... the form was preventing the display of the alert. 编辑好吧,我刚刚解决了与脚本2从脚本1.悲伤拿起触发事件说,这是我的一个基本的错误问题...的形式防止警报的显示。 However I still cannot get the is(:visible) to work. 但是我仍然不能使is(:visible)工作。

Your syntax may be off: 您的语法可能已关闭:

$("#cancellation_Form_Wrapper").is(":visible", function(){
    // do stuff
});

should be: 应该:

if($("#cancellation_Form_Wrapper").is(":visible")){
    // do stuff
});

EDIT: If you want something to happen after a div is made visible, you may want to use the show() callback rather than toggling visibility: 编辑:如果您希望使div可见发生某些事情,则可能要使用show()回调而不是切换可见性:

$('#cancellation_Form_Wrapper').show(timeInMilliseconds, function(){
    // do something
});

However, this needs to take place in the same function, which I don't think improves your position. 但是,这需要在相同的功能中进行,我认为这不会提高您的位置。

The problem is probably that your second on() script is firing at the same time as your first, meaning the wrapper is not yet visible. 问题可能是您的第二个on()脚本与第一个脚本同时触发,这意味着包装器尚不可见。 You could try wrapping the second on() in a short timeout: 您可以尝试在较短的超时时间内包装第二个on()

$(".booked").on("click", function() {
    setTimeout(function(){
        if($("#cancellation_Form_Wrapper").is(":visible")){
            // do stuff
        });
    }, 100);
});

That should introduce enough of a delay to make sure the wrapper has been shown before trying to execute the second statement. 这应该引入足够的延迟,以确保在尝试执行第二条语句之前已经显示了包装器。

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

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