简体   繁体   中英

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. 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.

The status system is basically a facebook clone. For the most part everything is supposed to function the way that facebook's status updates and status comments do. 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.

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.

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. 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. I also do not want to bother running the slideup logic if the user has clicked on a link. 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.

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. 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. 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.

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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