event.stopPropagation()无法在firefox javascript中运行

时间:2016-05-06 02:59:04

标签: javascript jquery firefox stoppropagation

event.stopPropagation()在Firefox浏览器中不起作用,但在谷歌浏览器或Internet Explorer或Opera中运行良好,点击btn_1时Firefox浏览器中的问题应该显示消息btn_1不显示div1 。还有其他功能或解决这个问题的方法吗?感激



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct task {
    int id;
    char ch;
    int times;
    struct task *next;
} Task;

typedef struct task_list {
    Task *head;
    Task *tail;
} TaskList;

typedef enum mode { NOP, WRITE, DELETE } Mode;

Task *new_task(int id, int freq, char aChar);
void add_taskList(TaskList *list, Task *task);
void update_taskList(TaskList *list, Task *task);
void drop_taskList(TaskList *list);
void fout_taskList(FILE *fp, TaskList *list);

int main(void) {
    char line[1024];
    char command[16];
    TaskList list = { NULL, NULL};
    FILE *fp;
    int i = 0;

    fp = fopen("input.txt", "r");
    while(fgets(line, sizeof line, fp)){
        int len, pos = 0;
        Mode mode = NOP;

        sscanf(line, " %15[^:]:%n", command, &len);
        pos += len;

        if(!strcmp(command, "write"))
            mode = WRITE;
        else if(!strcmp(command, "delete"))
            mode = DELETE;
        else
            continue;//ignore

        int freq = 0;
        char aChar;
        while(2 == sscanf(line + pos, "%d %c%n", &freq, &aChar, &len)){
            if(aChar == '\\'){
                char esc_char = line[pos + len];
                len += 1;
                switch(esc_char){
                case 'n':
                    aChar = '\n';
                    break;
                case 't':
                    aChar = '\t';
                    break;
                case ' ':
                    aChar = ' ';
                    break;
                }
            }
            Task *task = new_task(++i, freq, aChar);
            if(mode == WRITE)
                add_taskList(&list, task);
            else //if(mode == DELETE)
                update_taskList(&list, task);

            pos += len;
        }
    }
    fclose(fp);
    fp = fopen("output.txt", "w");
    fout_taskList(fp, &list);
    fclose(fp);

    drop_taskList(&list);
    return 0;
}

Task *new_task(int id, int freq, char aChar){
    Task *new_task = malloc(sizeof(*new_task));

    if(new_task){
        new_task->id    = id;
        new_task->times = freq;
        new_task->ch    = aChar;
    }
    return new_task;
}

void add_taskList(TaskList *list, Task *task){
    if(list->head){
        list->tail = list->tail->next = task;
    } else {
        list->head = list->tail = task;
    }
}
void update_taskList(TaskList *list, Task *task){
    Task *p = list->head;
    while(p){
        if(p->ch == task->ch){
            p->times -= task->times;//if(p->times < 0) p->times = 0;
            if(task->id > 0){//Execute only the first found element
                break;
            }
        }
        p = p->next;
    }
}
void drop_taskList(TaskList *list){
    Task *p = list->head;
    while(p){
        Task *tmp = p->next;
        free(p);
        p = tmp;
    }
    list->head = list->tail = NULL;
}
void fout_taskList(FILE *fp, TaskList *list){
    char prev = '\n';
    Task *p = list->head;
    while(p){
        for(int i = 0; i < p->times; ++i){
            fputc(prev = p->ch, fp);
        }
        if(prev != '\n')
            fputc(' ', fp);
        p = p->next;
    }
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您需要将event对象显式传递给回调函数。例如:

document.querySelector("body").onclick = function(e){
    console.log(e); // the current event
};

您正在利用Chrome将当前事件公开为全局on the window(即window.event或仅event)这一事实。 Firefox不会这样做 - other browsers也会受到影响。

答案 1 :(得分:1)

在我的情况下,致电event.preventDefault()是可行的。

我有一个React应用,当时我在material-ui复选框的onclick处理程序中调用event.stopPropogation(),如下所示:

<StyledCheckBox
  icon={<CircleUnchecked />}
  checkedIcon={<CircleChecked />}
  checked={shipmentEditIds.includes(id)}
  onClick={event => {
    event.stopPropagation();
    setShipmentEditIds({ id });
  }}
/>

由于某种原因,仅在firefox浏览器中,这并没有阻止事件的传播。 将event.preventDefault()添加到onClick处理程序后,它便解决了该问题。

我知道这不能为stopPropagation()为什么不能在Firebox浏览器中工作而提供答案,而只是分享对我有用的东西。

答案 2 :(得分:-1)

从onclick传递事件:

btn.onclick =  function(){delete_cooke1(this, event) ;} ;

并在event

中使用stopPropagation()参数
function delete_cooke1(mmm, e){
     e.stopPropagation();
     e.preventDefault(); // Add this line also
     var str =    mmm.id.toString() ;
     alert(str);
     return;
}