【问题标题】:Dialog Click listener not triggering in IE8 or Firefox with jQuery对话框单击侦听器未在 IE8 或 Firefox 中使用 jQuery 触发
【发布时间】:2011-10-07 17:48:28
【问题描述】:

我有这个点击监听器,但由于某种原因它没有在 IE8 或 Firefox 中触发:

console.log("listener attached");

jQuery(".ui-button-text").click(function() {

        console.log("this should have triggered");

        var ajaxUrl = '/ajax.php?popup=true';

        var dataString = "param="+param+"&param2="+param2;

        // contruct the ajax request
        jQuery.ajax({
            url: ajaxUrl, 
            dataType: 'json', 
            data: dataString, 
            beforeSend: function() {
                jQuery(".ui-button-text").html("Saving...");
            },
            complete: function() {
                jQuery(".ui-dialog-content").dialog("close");
            },
            success:function(response){

            } 
        });   

    });

所以我可以在控制台中看到“附加的侦听器”,但我没有看到点击触发器,这在 chrome 中有效,我在这里做错了什么?

谢谢!

更新:我尝试使用 live("click", function()... 代替,但它没有触发

UPDATE:所以另一个Update,我应该提到这个对话框的内容是通过单独的页面获取的。它是用 AJAX 加载的,这个动态加载的内容包含这个点击监听器。

更新:这是加载内容的代码,请注意我实际上并没有编写这段代码,所以我不完全理解为什么它会按照这里的方式完成:

        <!-- START OF NEW WINDOW POPUP -->
        jQuery('.option_window').click(function(){
            var url = jQuery(this).attr('href');
            var title = jQuery(this).attr('title');
            jQuery('<div />').dialog(
            {
                autoOpen: false,
                width: 720,
                title: "Manage Code",
                modal: true,
                buttons:{ 
                    "Save and Return":function() {
                        var self = this;

                        var popupForm = jQuery("form.submit_on_close");
                        //if( jQuery("form.submit_on_close").attr('action') != '#' || jQuery("form.submit_on_close").attr('action') != '') {
                        if(popupForm.attr('action') != '#' || popupForm.attr('action') != '') {
                            jQuery.ajax({
                                  url: jQuery("form.submit_on_close").attr('action'),
                                  dataType: 'json',
                                  data: jQuery("form.submit_on_close").serialize(),
                                  success: function(data) {     
                                        data = eval(data);
                                        if(data.resp == "success") { 
                                            var obj = jQuery('#repl_activation_row');
                                            obj.unbind('mouseover');
                                            if( data.property_code > 0) {
                                                if( obj.hasClass('codeoff') ) {
                                                    obj.removeClass('codeoff').addClass('codeon');
                                                }
                                            } else {

                                                if( obj.hasClass('codeon') ) {
                                                    obj.removeClass('codeon').addClass('codeoff');
                                                }

                                            }
                                        }
                                        jQuery(self).dialog('close');
                                    }
                                });
                        }
                        else 
                            jQuery(self).dialog('close');
                    }
                },
                //title:title,
                open: function(event, ui){ 

                    jQuery(".ui-dialog").delay(600).queue(function(n) {
                        var topPos = jQuery(".ui-dialog").offset().top;
                        var finalPos = topPos - (jQuery(".ui-dialog").height() / 3);
                        jQuery(".ui-dialog").css("top", finalPos);
                    n();
                    });



                    var self = this; 
                    jQuery.getJSON(url, {}, function(data){ 
                        jQuery(self).html(data); 
                    });
                },
                close: function(event, ui){ jQuery(this).dialog( "destroy" ); jQuery(this).remove(); }
            }).dialog('open'); 
            return false;
        })
        <!-- END OF NEW WINDOW POPUP -->

这是链接:

<a href="/popupmanager.php?code=3212&client=4432" class="actions option_window menulink">Manage</a>

【问题讨论】:

  • 您是否可能忘记将代码包装在 $(document).ready() 或类似函数中?
  • 你能把它托管在 jsfiddle 上吗?你也能告诉我你想在什么控件上添加监听器
  • 感谢 Clive 的回复,我已经将它包裹在 jQuery(document).ready(function() {
  • ui-button-text 元素是一个位于 jQuery UI 对话框弹出窗口中的按钮
  • 我一直在尝试设置一个,但是 jsfiddle 是否支持将不同的页面加载到对话框中?当我加载在同一页面上定义 div 的普通对话框时,使用 live() 时会触发侦听器

标签: jquery firefox internet-explorer-8 click


【解决方案1】:

您的错误是由 jQuery UI button() 方法的错误实现/假设引起的。相关代码如下所示并解释(请参阅答案底部的修复):

HTML:        <button id="save">Save and Return</button>

JavaScript:  $("#save").button();

这段代码的输出如下:

<button id="save" class="ui-button ... ui-button-text-only" role="button" ..>
    <span class="ui-button-text">Click me</span>
</button>

如您所见,.ui-button-text 类的元素是 &lt;button&gt; 元素的子元素。
现在,看看 this fiddle。在几乎所有浏览器中,小提琴都表明不会在 &lt;button&gt; 元素的子元素上触发任何事件。

修复代码

要修复您的代码,请将jQuery(".ui-button-text").click(function() { 替换为以下任一:

jQuery(".ui-button").click(function() {               // Recommended
jQuery(".ui-button-text").parent().click(function(){  // Alternative method

检查这个comparison of the methods(小提琴),你会发现错误是由你错误的实现/假设jQuery UI插件引起的。

链接:

  • 小提琴:Testing event listeners 在大多数浏览器中,这个小提琴表明按钮子元素的事件侦听器触发。
  • 小提琴:Solution - 您的代码与修补代码的比较

【讨论】:

    【解决方案2】:

    我想通了,我需要将监听器附加到 ui-button:

    jQuery(".ui-button").live("click", function() {
    

    不是

    jQuery(".ui-button-text")
    

    我不知道为什么会这样,我不敢相信我花了这么长时间才弄清楚,对不起,伙计们,希望我能给你们中的一个人加分..

    【讨论】:

      【解决方案3】:

      尝试使用livequery,它会略有不同,然后即使通过 ajax 更改也会触发它

      http://plugins.jquery.com/project/livequery

      jQuery(".ui-button-text").livequery(function(){
        $(this).click(function(){...});
      })
      

      【讨论】:

        【解决方案4】:

        看起来这可能是一种竞争条件,您试图在按钮被添加到 dom 之前连接它们。也许 chrome 比其他浏览器更快地将 dom 组合在一起。

        在您确定对话框是 html 之后,将您的按钮处理代码移动到。

        jQuery('.option_window').click(function(){
                var url = jQuery(this).attr('href');
                var title = jQuery(this).attr('title');
                jQuery('<div />').dialog(
                {
                    autoOpen: false,
                    width: 720,
                    title: "Manage Code",
                    modal: true,
                    buttons:{ 
                        "Save and Return":function() {
                            var self = this;
        
                            var popupForm = jQuery("form.submit_on_close");
                            //if( jQuery("form.submit_on_close").attr('action') != '#' || jQuery("form.submit_on_close").attr('action') != '') {
                            if(popupForm.attr('action') != '#' || popupForm.attr('action') != '') {
                                jQuery.ajax({
                                      url: jQuery("form.submit_on_close").attr('action'),
                                      dataType: 'json',
                                      data: jQuery("form.submit_on_close").serialize(),
                                      success: function(data) {     
                                            data = eval(data);
                                            if(data.resp == "success") { 
                                                var obj = jQuery('#repl_activation_row');
                                                obj.unbind('mouseover');
                                                if( data.property_code > 0) {
                                                    if( obj.hasClass('codeoff') ) {
                                                        obj.removeClass('codeoff').addClass('codeon');
                                                    }
                                                } else {
        
                                                    if( obj.hasClass('codeon') ) {
                                                        obj.removeClass('codeon').addClass('codeoff');
                                                    }
        
                                                }
                                            }
                                            jQuery(self).dialog('close');
                                        }
                                    });
                            }
                            else 
                                jQuery(self).dialog('close');
                        }
                    },
                    //title:title,
                    open: function(event, ui){ 
        
                        jQuery(".ui-dialog").delay(600).queue(function(n) {
                            var topPos = jQuery(".ui-dialog").offset().top;
                            var finalPos = topPos - (jQuery(".ui-dialog").height() / 3);
                            jQuery(".ui-dialog").css("top", finalPos);
                        n();
                        });
        
        
        
                        var self = this; 
                        jQuery.getJSON(url, {}, function(data){ 
                            jQuery(self).html(data); 
                            //NOT SURE WHY YOU ARE USING .getJSON TO GET WHAT LOOKS LIKE HTML, BUT IF THAT WORKS, I'LL LEAVE IT ALONE
                            //PUT THE BUTTON STUFF HERE:
                                jQuery(".ui-button-text").click(function() {
        
                                    console.log("this should have triggered");
        
                                    var ajaxUrl = '/ajax.php?popup=true';
        
                                    var dataString = "param="+param+"&param2="+param2;
        
                                    // contruct the ajax request
                                    jQuery.ajax({
                                        url: ajaxUrl, 
                                        dataType: 'json', 
                                        data: dataString, 
                                        beforeSend: function() {
                                            jQuery(".ui-button-text").html("Saving...");
                                        },
                                        complete: function() {
                                            jQuery(".ui-dialog-content").dialog("close");
                                        },
                                        success:function(response){
        
                                        } 
                                    });   
        
                                });
        
                        });
                    },
                    close: function(event, ui){ jQuery(this).dialog( "destroy" ); jQuery(this).remove(); }
                }).dialog('open'); 
                return false;
            })
            <!-- END OF NEW WINDOW POPUP -->
        

        希望有帮助!

        【讨论】:

          【解决方案5】:

          console.log 有时在 IE 上不起作用,尤其是当您不使用某种开发工具时。可能是你的错误?

          【讨论】:

          • 感谢您的建议,但这不是问题,我已经启动了 Firebug,并且可以看到第一个控制台日志“已连接侦听器”
          【解决方案6】:

          这有帮助吗add javascript into a html page with jquery

          您可能在将脚本动态加载到页面中时遇到问题。

          【讨论】:

            【解决方案7】:

            我会通过让 ajax.php 做一些事情(比如将日志写入 txt)来开始调试,以查看它是否被调用,如果调用了,输出是什么。

            更新您的更新:如果事件侦听器来自其他地方,您应该做的第一件事是在控制台中运行代码,这样您就可以确定代码运行正常......或者您可以只是 `console.log( '事件处理程序被触发')

            编辑:更清楚地了解代码的上下文。您发布的代码的第二部分首先加载?如果是这种情况,第一部分应该使用 dataType: 'script', 来加载第二部分,但这意味着重构代码

            【讨论】:

            • 这个加载的内容确实加载了,因为我在日志中看到“listener attach”,这个脚本是从加载的脚本中执行的。
            • 但是你看到“这应该触发”了吗?
            • 在 chrome 中我会,但在 FF 和 IE 中我不会
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-14
            • 1970-01-01
            • 1970-01-01
            • 2013-12-05
            • 1970-01-01
            相关资源
            最近更新 更多