好友列表功能

Friendlist to function

本文关键字:功能 列表 好友      更新时间:2023-09-26

我想调用一个jQuery函数,并通过load()将userinfo加载到div中。这是我的用户列表:

<a href="#" id="1" class="profile"><b>Alex</b><br>Some infos...</a>
<a href="#" id="2" class="profile"><b>Barney</b><br>Some infos...</a>
<a href="#" id="3" class="profile"><b>Chris</b><br>Some infos...</a>

这是我的第一次尝试:

$(".profile").click(function(){
$("#title").text(".profil b");
$("#profilediv").load("user.php?id="+#id);});

当然,这是行不通的。

它应该做什么:

1)在点击的个人资料链接中获取-tag的值(用户名),并将其放入 #title 中。

2)将用户ID发送给用户.php并将内容放入 #profilediv

我不知道这是否是最好的方法...也许这样的事情也可以工作:

<a href="#" id="1,Alex" class="profile">Alex<br>Some infos...</a>

然后将 id 拆分为用户 ID 和用户名...

感谢您的帮助!

你可以data属性添加到你的html中,比如

<a href="#" data-id="1" data-name="Alex" class="profile"><b>Alex</b><br />Some infos...</a>
<a href="#" data-id="2" data-name="Barney" class="profile"><b>Barney</b><br />Some infos...</a>
<a href="#" data-id="3" data-name="Chris" class="profile"><b>Chris</b><br />Some infos...</a>

然后将值放入脚本中,例如

$(".profile").click(function () {
    var name = $(this).data("name");
    var id = $(this).data("id");
    $("#title").text(name);
    $("#profilediv").load("user.php?id=" + id);
});

如何改为创建一个函数:

<a href="#" onClick='javascript:loadProfile("1","Alex");'>
    <b>Alex</b><br>Some infos...
</a>
<a href="#" onClick='javascript:loadProfile("2","Barney");'>
    <b>Barney</b><br>Some infos...
</a>
<a href="#" onClick='javascript:loadProfile("3","Chris");'>
    <b>Chris</b><br>Some infos...
</a>

和JavaScript:

function loadProfile(id,name){
    $("#title").text(name);
    $("#profilediv").load("user.php?id="+ id);});
}