【问题标题】:jQuery click event is not working in Internet ExplorerjQuery 单击事件在 Internet Explorer 中不起作用
【发布时间】:2023-03-28 22:15:02
【问题描述】:

此代码基于单击激活隐藏单选按钮的图像。注册数据库的答案, 但是它在除 Internet Explorer 之外的所有浏览器上都可以正常工作。任何建议都会被采纳

    main : function(){
        var thisoptions = this.options;
        // Set up page:
        // Add grey borders
        $("input:radio").each (function () {
            var spanId = "imageBox_"+$(this).attr("id");
            $("#"+spanId).addClass('greyBorder');
         })
        // Pre-set already checked boxes with the highlight class
        $("input:radio:checked").each(function () {
            var spanId = "imageBox_"+$(this).attr("id");
            $("#"+spanId).removeClass('greyBorder');
            $("#"+spanId).addClass('imgChecked');
        })

        // Highlight image when clicked
        $(".imageBox_"+thisoptions.q_id).click(function(){
            $(".imageBox").removeClass('imgChecked');
            $(".imageBox").addClass('greyBorder');
            $(this).removeClass('greyBorder');
            $(this).addClass('imgChecked');
        });

        // Highlight image when textbox used
        $("input[type='text']").focus(function(){
            var spanId = "imageBox_"+ $(this).attr('data-for');
            $(".imageBox").removeClass('imgChecked');
            $(".imageBox").addClass('greyBorder');
            $("#"+spanId).removeClass('greyBorder');            
            $("#"+spanId).addClass('imgChecked');
        });
    }
  };

在 Internet Explorer 中,当我按下图像时,所有边框颜色都会发生变化,并且一切看起来都已被选中,但未选中隐藏的单选按钮。即使它在所有其他浏览器中都能正常工作!

【问题讨论】:

    标签: jquery html css internet-explorer


    【解决方案1】:

    尝试将事件处理程序更改为:

    $('body').on('click','your image selector',function(){
    your code goes here...
    });
    

    【讨论】:

    • 为什么是...?
    • 这就像 IE 不识别图像,有点解释它因为它还没有加载 DOM,所以通过执行上面的代码,你将事件监听器附加到 body 而不是直接元素:)
    • "在 Internet Explorer 中当我按下图像时所有边框颜色都会改变"
    • 这是默认的焦点动作,但是jquery事件还没有执行,不是吗?在函数中注入警报可能也不会执行
    • @DireWolf 我猜选择器有问题试试:$('body').on('click',".imageBox_"+thisoptions.q_id, function(){
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2012-06-07
    相关资源
    最近更新 更多