简体   繁体   English

IE中的event.target问题

[英]Problem with event.target in IE

I'm writing js for a status update system to be used on various pages throughout a app that I'm working. 我正在为状态更新系统编写js,以便在我正在使用的整个应用程序的各个页面上使用。 I am really just starting to get more comfortable with javascript so it has been somewhat of a challenge to get to the point where I have everything now. 我真的刚开始对JavaScript更加熟悉,因此要达到现在我拥有的一切已经有些困难。

The status system is basically a facebook clone. 状态系统基本上是一个Facebook克隆。 For the most part everything is supposed to function the way that facebook's status updates and status comments do. 在大多数情况下,一切都应该以Facebook状态更新和状态注释的方式起作用。 The intended behavior is that when the user clicks in the status textarea, the div under the status textarea slides out revealing the submit button as well as some other checkboxes. 预期的行为是,当用户单击状态文本区域时,状态文本区域下的div会滑出,显示“提交”按钮以及一些其他复选框。

If the user clicks anywhere else on the page except a link or any element that has the class prevent_slideup the div slides up hiding the submit button and any checkboxes. 如果用户单击页面上除链接或具有prevent_slideup类的任何元素之外的其他任何位置,则div会向上滑动,从而隐藏提交按钮和所有复选框。

I'm using a document.body click function to determine what the user clicked on so I know which form elements to hide if I should even hide them. 我正在使用document.body click函数来确定用户单击的内容,所以我知道要隐藏哪些表单元素,甚至可以隐藏它们。 I do not want this slideup to take place on a textarea if that textarea has focus or the user is selecting a checkbox that goes with that form. 如果文本区域具有焦点,或者用户选择了与该表单一起使用的复选框,则我不希望在文本区域上发生此幻灯片放大。 Hence the prevent_slideup class. 因此,prevent_slideup类。 I also do not want to bother running the slideup logic if the user has clicked on a link. 如果用户单击链接,我也不想打扰运行Slideup逻辑。 I'd prefer they just leave the page without having to wait for the animation. 我希望他们只是离开页面而不必等待动画。

The code that I was using to accomplish this task can be found in the $(document.body).click(function (e) section below where I'm doing a .is('a') check on the event target. 我用于完成此任务的代码可以在下面的$(document.body).click(function(e)部分)中找到,该部分中我正在对事件目标进行.is('a')检查。

This code works as expected in chrome and firefox, however in ie when a link is clicked for the first time it seems that the element stored in var target is actually a div instead of an anchor. 此代码在chrome和firefox中可以按预期工作,但是在第一次单击链接时,似乎存储在var target中的元素实际上是div而不是锚。 What ends up happening is that the submit div slides up and the user is not taken to the link that they just clicked on. 最终发生的事情是,提交div滑动并且用户没有被带到他们刚刚单击的链接。 If a link is clicked a second time the user is taken to the page as you would expect. 如果第二次单击链接,则将用户带到该页面,如您所愿。

It seems to me that there's some kind of a lag in ie as to what the current event being fired is. 在我看来,在当前事件触发方面存在某种滞后。

The entire status module is working other than this one strange ie bug regarding the users click on the link not being carried out the first time that they click a link after opening the status textarea. 整个状态模块都在工作,而不是一个奇怪的问题,即关于用户在打开状态文本区域后第一次单击链接时未单击链接的错误。 Does anything jump out in this script that would explain this behavior or does anyone have any other advice? 该脚本中是否有任何内容可以解释这种行为,或者有人有其他建议吗?

Thanks in advance for your help. 在此先感谢您的帮助。

$(document).ready(function(){
    $("textarea.autoresize").autoResize();
});

$(document.body).click(function (e){
    var target = e.target || e.srcElement;
    console.log(target);
    console.log($(target).is('a'));
    if($(target).hasClass('prevent_slideup') || $(target).is('a'))
    {
        return true;
    }
    else
    {
        var active_element = document.activeElement;
        var active_status_id = $(active_element).attr('data-status_id');
        var active_has_data_status_id = (typeof active_status_id !== 'undefined' && active_status_id !== false) ? true : false;

        $('textarea').each(function(){
            if($(this).hasClass('status_comment_textarea'))
            {
                var status_id = $(this).attr('data-status_id');
                if($('#comment_textarea_'+status_id).val() === '' && (!active_has_data_status_id || active_status_id !== status_id))
                {
                    hide_status_comment_submit(status_id);
                }
            }
            else if($(this).attr('id') === 'status_textarea')
            {
                if($('#status_textarea').val() === '' && $(active_element).attr('id') !== 'status_textarea')
                {
                    $('#status_textarea').html($("#status_textarea").attr('placeholder'));
                    hide_status_submit();
                }
            }
        });

        return true;        
    }
});

$("#status_textarea").live('click', function(){
    if($('#status_textarea').val() === $("#status_textarea").attr('placeholder'))
    {
        $('#status_textarea').html('');
    }
    show_status_submit();
    return false;
});

$(".comment_toggle").live('click', function(){
    var status_id = $(this).attr('data-status_id');
    show_status_comment_submit(status_id);
    return false;
});

$(".status_comment_submit").live('click', function(){
    var status_id = $(this).attr('data-status_id');
    $('#status_comment_submit_wrapper_'+status_id).addClass('status_comment_submit_successful');
    return false;
});

$(".show_hidden_comments").live('click', function(){
    var status_id = $(this).attr('data-status_id');
    $('#status_hidden_comments_'+status_id).show();
    $(this).hide();
    return false;
});

function hide_status_submit()
{
    $("#status_textarea").removeAttr('style');
    $("#status_textarea").blur();
    $("#status_block").removeClass('padding_b10');
    $("#status_submit_wrapper").slideUp("fast");
    return false;
}

function show_status_submit()
{
    if ($("#status_submit_wrapper").is(":hidden"))
    {
        $("#status_block").addClass('padding_b10');
        $("#status_submit_wrapper").slideDown('fast');
    }
    return false;
}

function hide_status_comment_submit(status_id)
{
    if(!$('#status_comment_submit_wrapper_'+status_id).is(":hidden"))
    {
        $('#status_comment_submit_wrapper_'+status_id).hide();
        $('#fake_comment_input_'+status_id).show();
        $('#comment_textarea_'+status_id).removeAttr('style');
    }
    return false;
}

function show_status_comment_submit(status_id)
{
    if($('#status_comment_submit_wrapper_'+status_id).is(":hidden"))
    {
        $('#fake_comment_input_'+status_id).hide();
        $('#status_comment_submit_wrapper_'+status_id).show();
        $('#comment_textarea_'+status_id).focus();      
    }
    return false;
}

function status_comment_submit_successful()
{
    hide_status_comment_submit($('.status_comment_submit_successful').attr('data-status_id'));
    $('.status_comment_submit_successful').removeClass('status_comment_submit_successful');
    return false;
} 

I figured out that there were two main issues with my script... 我发现我的脚本有两个主要问题...

1.) The document.body function and the #status_textarea live click funtioins were conflicting with each other. 1.) document.body函数和#status_textarea实时单击功能相互冲突。

2.) After adding the logic for the #status_textarea function into the document.body function I noticed that the script still didn't quite work as expected in internet explorer unless I had an alert in the function. 2.)在将#status_textarea函数的逻辑添加到document.body函数中之后,我注意到该脚本仍然无法按预期在Internet Explorer中正常工作,除非我对该函数发出警报。 The problem at this point was that the autoresize plugin that I'm using on the textarea was also conflicting with the document.body function. 此时的问题是,我在textarea上使用的autoresize插件也与document.body函数冲突。

I was able to rectify the situation by adding a dummy text input and hiding the status textarea. 通过添加虚拟文本输入并隐藏状态文本区域,我能够纠正这种情况。 On click of the dummy text input the status textarea is shown and the the dummy text input is hidden. 单击虚拟文本输入后,将显示状态文本区域,并且虚拟文本输入被隐藏。 I have no idea why this worked, but it seems to have solved my problems. 我不知道为什么这行得通,但似乎已经解决了我的问题。

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

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