Using jQuery to #39;click#39; a li element(使用 jQuery 来“点击一个 li 元素)
问题描述
我有一个 <ul>
元素,它动态生成 <li>
元素,并且只想运行 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,但它似乎不起作用 - 知道我做错了什么吗?
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");
});
推荐答案
如果您添加动态,请点击列表但选择项目:
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 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!