jQuery在有2时选择div。 [英] jQuery selecting divs when there are 2.

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

问题描述

先告诉你一些我的脚本。它创建了朋友请求的php / html呈现。您可能知道,一次可以有1到多个朋友请求。现在我的jQuery脚本只适用于第一个,所以我需要一些指导来使两个到多个功能正常工作。

Let tell you a bit about my script first. It creates a php/html render of the friend requests. As you may know, there can be 1 to many friend requests at a time. Right now my jQuery script only works for the first so I do need some guidance to get the two to many functionality working.

请注意,我的div对每个人都有不同的ID。

Notice that my div's have a different id for each person.

这里是我的html

    <div class='fRequest'>
<h3>Pending Friend Requests:</h3><div class='friendRequest' id='0'><img src='[url]' alt='Charles Williamson'/> Charles Williamson<a id='4' class='friendConfirm' href='#' 
        style='border:1px solid #dadada; background:#fff; margin-left: 
        10px; line-height: 60px; padding: 4px 4px; color:gray; text-decoration:none;'>
        Confirm</a></br></div><div class='friendRequest' id='1'><img src='[url]' alt='Rachel Cole'/> Rachel Cole<a id='5' class='friendConfirm' href='#' 
        style='border:1px solid #dadada; background:#fff; margin-left: 
        10px; line-height: 60px; padding: 4px 4px; color:gray; text-decoration:none;'>
        Confirm</a></br></div></div>

第二个是我的jQuery。

Second here's my jQuery.

$(document).ready(function(){
    $(".friendAdded").css('display', 'none');

    var frid = $(".friendConfirm", ".friendConfirm").attr('id');

    $(".friendConfirm#"+frid).click(function(){

        $.get("JSON/addFriend.php?fid="+frid,
            function(data){
            $(".friendAdded").append(data);
            $(".friendAdded").show() })
    });
    $(".closeOwe").click(function(){
        $(".friendAdded").css('display', 'none')
        location.reload();
        });

});

如何使此代码适用于两个或多个朋友请求。

How can I make this code work for both or many friend requests.

PS。我对jquery还很新,所以还在学习。无法为此找到教程。

PS. i'm fairly new to jquery so still learning. Having trouble finding tutorials for this.

我可以获得任何帮助。

推荐答案

.click()事件处理程序绑定到具有类 friendConfirm 的所有元素,然后连接点击元素的 id ,由 this.id 引用,到 $。get() url:

Bind the .click() event handler to all elements with the class friendConfirm, and then concatenate the clicked element's id, referenced by this.id, to the $.get() url:

$(document).ready(function(){
    $(".friendAdded").css('display', 'none');

    $(".friendConfirm").click(function() {
        $.get("JSON/addFriend.php?fid="+this.id,
            function(data) {
              $(".friendAdded").append(data);
              $(".friendAdded").show() 
            }
        );
    });

    $(".closeOwe").click(function() {
        $(".friendAdded").css('display', 'none')
        location.reload();
    });
});​

这篇关于jQuery在有2时选择div。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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