豁免表单的可点击行为

Exempt form from clickable behavior

本文关键字:表单      更新时间:2024-02-24

目前,我有一些文本框被包围在一个展开的顶部栏中,也就是说,网页顶部的一条带,当单击时,它可以切换以下展开/关闭行为。

var toggle = true;
$("#expandable").click(function() {
    if (toggle == true){
        $(this).stop().animate({"height":"300px"},500);
        toggle = false;
    } else {
        $(this).stop().animate({"height":"50px"},500);
        toggle = true;
    }
});

现在,我唯一的问题是,当我点击文本框时,展开的div相信它被点击了(这很公平)。但是当我点击表单时,我不希望顶部的栏关闭。

如有任何帮助,我们将不胜感激。

如果事件发生在输入元素上,则防止其冒泡

$("#expandable :input").click(function(e) {
    e.stopPropagation();
});

这样,单击事件将不会到达#expandable元素上的单击处理程序。

特殊选择器:input匹配输入、文本区域和选择框。

var toggle = true;
$("#expandable").off('click');
$("#expandable").on('click',function() {
    if (toggle == true){
        $(this).stop().animate({"height":"300px"},500);
        toggle = false;
    } else {
        $(this).stop().animate({"height":"50px"},500);
        toggle = true;
    }
});

你也可以这样写来防止冒泡。