启用仅单击一个子jquery

时间:2015-05-05 11:10:50

标签: javascript jquery html

所以我有以下html

<li clas='k-item'>
<input type='checkbox'>
<span>ABC</span>
</li>

我有什么方法可以点击 input并停用spanli

这是我尝试过的,但它似乎无法正常工作

$('.k-item').on('click', function (e) {
     $(this).unbind('click');
            if ($(e.target).is('input')) {
                $('input').bind('click');
            }
});

我对jquery没什么经验

4 个答案:

答案 0 :(得分:2)

您需要使用:

$('.k-item input').click(function(){
   //modify the rest handler code...
});

更新:如果为li定义了click,并且您不希望在子跨度上触发,那么您将需要停止事件传播。

$('.k-item span').click(function(e){
    e.stopPropagation();//do not trigger parent click on child span
});

答案 1 :(得分:1)

为什么不直接定位input?然后你可以使用e.stopPropagation()来防止LI获得点击事件(这是我收集你的目标):

$('.k-item input').click(function (e) {
    // Stop the LI from firing a click event
    e.stopPropagation();
    // input is clicked
});

答案 2 :(得分:0)

你只需要这个:

#include <iostream>

namespace detail {
    struct writer
    {
        template<class T>
        void operator()(const T& t) {
            if (_first) {
                _first = false;
            }
            else {
                std::cout << ", ";
            }
            std::cout << t;
        }

    private:
        bool _first = true;
    };

    // non-template overload to catch no-parameter case
    void do_write(writer&&)
    {

    }

    // general case. Note w is passed by r-value reference
    // to allow the caller to construct it in-place        
    template<typename T, typename...Ts>
    void do_write(writer&& w, const T& t, Ts&&...ts)
    {
        w(t);
        do_write(std::forward<writer>(w), std::forward<Ts>(ts)...);
    }


}

// method 1 - no recursion
template<typename... Ts>
void WriteLog1(Ts&&... Vals) {
    // expand one call for each parameter
    // use comma operator to ensure expression result is an int
    detail::writer write;

    using expander = int[];
    expander { 0, (write(std::forward<Ts>(Vals)), 0)... };

    // write the final linefeed
    std::cout << std::endl;
}

// method 2 - recursion

template<typename...Ts>
void WriteLog2(Ts&&...ts)
{
    detail::do_write(detail::writer(), std::forward<Ts>(ts)...);
    std::cout << std::endl;
}

int main() {
    WriteLog1("apple", "orange", "mango");
    WriteLog1("apple", "orange");
    WriteLog1("apple");
    WriteLog1("apple", 1.0, "orange", 1L, "mango", 2.6f);
    WriteLog1(); // test pathalogical case

    WriteLog2("apple", "orange", "mango");
    WriteLog2("apple", "orange");
    WriteLog2("apple");
    WriteLog2("apple", 1.0, "orange", 1L, "mango", 2.6f);
    WriteLog2(); // test pathalogical case
    return 0;
}

答案 3 :(得分:0)

只需选择所需的元素并应用事件监听器:

static = 'http://www.example.com/'
end = ['abc','def']

{ k:'{}{}'.format(static, v) for k,v in enumerate(end) }
相关问题