Using jQuery to 'click' a li element

拜拜、爱过 提交于 2019-11-30 08:48:05

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

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

try on the fiddle: http://jsfiddle.net/emPKS/

Remove the space in selector

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

and You can also use .on to attach the specific event to the matched elements

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

Js Fiddle

Use .on()

As your content is added dynamically so it is not accessible directly ,So you have to use Event delegation.

$('#results').on('click','li.searchterm',function() {    
    console.log('testing');
});

Either do

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

OR

<li class="device_result searchterm" data-url="apple-iphone-5s" onclick="clickFunc(1)">
   <a href="#">Apple iPhone 5s</a>
</li>
<li class="device_result searchterm" data-url="apple-iphone-5c" onclick="clickFunc(2)">
   <a href="#">Apple iPhone 5s</a>
</li>

And the Javascrip

function clickFunc(id) {
   console.log('Do something with ' + id);
}
$( "li.searchterm" ).on('click',function() {    
   console.log('testing');
});

You can do this without assigning id to ul

$('ul').on('click','li.searchterm',function(){
//do your stuffhere;
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!