jQuery类选择器不工作 [英] jQuery Class selector not working

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

问题描述



有问题的我的html部分看起来像是一样的这... ...

 < div id =foo> 
< a class ='bar'href ='#'>下一步< / a>
< / div>

jQuery部分如下。

<点击(功能()
{
alert(CLICKED);
}); pre> $('。bar'

我的问题是我无法得到这个提示,我想我正在选择班级下一个,但不会因为某种原因而选择它。我也尝试了几乎所有关于这个页面,但没有任何工作。如果我不试图指定锚点标记,例如$('#foo')。click(function()...然后它可以工作,但是在这个div中会有多个锚点标记,所以只需要在点击的div不会用于我需要的东西,这个网站是一个使用ajax发送信息到do_search.php的搜索引擎,在do_search.php中,我根据搜索到的结果做出分页决定,如果适用的话,可能会创建并回显下一个,上一个,最后一个和第一个链接。编辑:我只是想通了它,它是我放置的.next函数,因为它并不是在初始文档加载时创建的,而是在返回结果后,我将.next函数移至了ajax函数的成功部分,因为如果需要的话,这些按钮将被创建,现在它工作。

解决方案

尝试使用 live()命令:



$(。bar)。live(click,func由于你通过AJAX加载你的按钮,click事件并没有绑定到它。如果使用live()命令,它将自动将事件绑定到页面加载后创建的所有元素。



更多详细信息, here


I'm struggling to make an alert come up when an anchor tag with a specific class is clicked inside of a div.

My html section in question looks like this...

<div id="foo">
  <a class='bar' href='#'>Next</a>
</div>

The jQuery section is as follows..

$('.bar').click(function()
{
  alert("CLICKED");
});

My problem is that I cannot get this alert to come up, I think that I'm properly selecting the class "next", but it won't pick it up for some reason. I've also tried almost everything on this page but nothing is working. If I don't try to specify the anchor tag i.e. $('#foo').click(function()... then it works, but there will be multiple anchor tags within this div, so simply having the alert executed when the div is clicked won't work for what I need. The website this is on is a search engine using ajax to send information to do_search.php. Within the do_search.php I make pagination decisions based on how many results are found, and if applicable, a next, previous, last, and first link may be made and echoed.

EDIT: I just figured it out, it was my placement of the .next function, since it wasn't created on the initial document load but instead after a result had been returned, I moved the .next function to the success part of the ajax function since that is where the buttons will be created if they need to be, now it works.

解决方案

Try using the live() command:

$(".bar").live("click", function(){ alert(); });

Because you load your button via AJAX, the click event isn't binded to it. If you use the live() command, it will automatically bind events to all elements created after the page has loaded.

More details, here

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

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