jQuery .click()问题 [英] jQuery .click() problem

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

问题描述

我hava一个简单的jQuery脚本。我使用.ajax()从我的数据库获取一些信息,它的工作完美。



问题是php代码genereate xhtml代码与a标记不同的类,例如.info,.delete和.edit。



当我点击我的.info链接时,我想做的东西。
我试过这个代码:

  $('a.info' 
e.preventDefault();
alert('Do stuff');
});

它根本不工作,不会发生。 firebug给我没有错误,所以我不知道如何解决问题。

  $('a')。click(function(e){
e.preventDefault();
alert('Do Stuff 2');
});

适用于网站上的所有链接,但不适用于从PHP代码生成的链接。 / p>

我的代码:

  $(function(){
$ .ajax({
type:POST,
url:'users.php',
data:'getall = true',
success:function {
$('#content').html(r);
},
错误:function(){
alert('Error');
}
});

$('a.info')click(function(e){
e.preventDefault();
alert ');
});


$('a')。click(function(e){
e.preventDefault();
alert('Do Stuff 2');
});

}) p>

解决方案

使用 .live

  $('a.info')。live('click',function(e){
e.preventDefault();
alert('Do stuff');
});


$('a')。live('click',function(e){
e.preventDefault();
alert ');
});


I hava a simple jQuery script. I use .ajax() to get some info from my database, it works perfect.

The problem is the php-code genereate xhtml code with a-tags with different class, like .info, .delete and .edit.

I want to do stuff when i click in my .info link. I have tried this code:

$('a.info').click(function (e) {
    e.preventDefault(); 
    alert('Do stuff');
});

it dosen't work at all, nothin happens. firebug gives me no error so i dont know how to solve the problem.

    $('a').click(function (e) {
    e.preventDefault(); 
    alert('Do Stuff 2');
});

works on all links on the site but not on the link that is generated from the php code.

my code:

$(function(){       
$.ajax({
    type: "POST",
    url: 'users.php',
    data: 'getall=true',
    success: function( r ) { 
        $(' #content ').html( r );
    },
    error: function( ) {
        alert('Error');
    }
});

$('a.info').click(function (e) {
    e.preventDefault(); 
    alert('Do Stuff');
});


$('a').click(function (e) {
    e.preventDefault(); 
    alert('Do Stuff 2');
});

});

解决方案

Use .live

$('a.info').live('click', function (e) {
    e.preventDefault(); 
    alert('Do stuff');
});


$('a').live('click', function (e) {
    e.preventDefault(); 
    alert('Do Stuff 2');
});

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

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