使用jQuery'点击'li元素 [英] Using jQuery to 'click' a li element

查看:227
本文介绍了使用jQuery'点击'li元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成< li> 元素的< ul> 元素,并且只需要运行 onclick 事件

I have a <ul> element that dynamically generates the <li> elements and simply want to run a onclick event

<ul id="results">
    <li class="device_result searchterm" data-url="apple-iphone-5s">
        <a href="#"> Apple iPhone 5s </a>
    </li>
    <li class="device_result searchterm" data-url="apple-iphone-5c">
        <a href="#"> Apple iPhone 5s </a>
    </li>
</ul>

我在 $(document).ready中获得以下jQuery block,但它似乎不工作 - 任何想法我做错了什么?

I've got the following jQuery in a $(document).ready block but it doesn't seem to work - any ideas what I'm doing wrong?

$("li .searchterm").click(function() {  
    console.log("testing");
});


推荐答案

如果您添加dinamically选择项目:

if you add dinamically put the click on the list but select the items:

$("#results").on("click", ".searchterm", function(event){
    console.log('clicked');
});

尝试一下小提琴: http://jsfiddle.net/emPKS/

这篇关于使用jQuery'点击'li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆