jQuery .click()不工作 [英] jQuery .click() not functioning

查看:109
本文介绍了jQuery .click()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已建立< ul> 清单,并且正在填入< li id =subsitem> 从jQuery函数。我试图建立一个点击功能,以便每当< li> 的点击,它就会消失。我使用 slideUp()函数。

I have built a <ul> list and it is being populated with <li id="subsitem"> from a jQuery function. I am trying to build a click function so that whenever one of those <li>'s is clicked, it disappears. I'm using the slideUp() function.

这是我的代码:

$("#subsitem").click(function() {
  // Act on the event
  $(this).slideUp();
}); 

这不起作用,但当我将它更改为

This doesn't work, yet when i change it to

$("li").click(function() {
  // Act on the event
  $(this).slideUp();
}); 

它工作,但是唯一的问题是它适用于我所有的 ; li> s。有人提出任何提示吗?

It works, but the only problem then is it works on all my <li>s. Anyone any tips?

()

$("#subs").append("<li><a href='#' class='subsitem'><div class='favicon'><img src='icon.png' width='16' height='16'></div>title</a></li>");


推荐答案

Ok使用jquery irc频道我正在寻找答案。

Ok having used the jquery irc channel i managed to get the answer i was looking for.

我需要使用.live()函数。这是我的代码现在看起来

I needed to use the .live() function. This is what my code now looks like

$("#subsitem").live('click',function(){
$(this).parent().slideUp();});

这篇关于jQuery .click()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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