我正在使用JS + jQuery中的twitter克隆进行开发程序的预备课程,所以如果这是一个明显的修复 - 让我知道! 我有一个问题我无法解决:"点击用户名,页面返回该用户的最后一条推文"。
我唯一能想到的是<a>
标签上的事件处理程序过滤页面。但是,我很缺乏经验,也不清楚如何继续。
有什么想法吗? 注意 - 为简洁起见,我删除了一些代码。
$(document).ready(function() {
var $body = $('body');
$body.html();
var stream = function() {
var index = streams.home.length - 1;
while (index >= 0) {
var tweet = streams.home[index];
var $tweet = $('<div class="tweetbody"></div>');
$tweet.text(': ' + tweet.message);
$tweet.appendTo($body);
index -= 1;
var link = $('<a>', {
text: tweet.user,
href: '#',
}).prop('outerHTML');
$tweet.html('@' + link + ': ' + tweet.message);
}
};
这里是<a>
代码事件:
//click on a username to see that user's timeline.
$('a').on('click', function() {
console.log("a tag is clicked ");
console.log(this);
});
}();
}); //end document ready body
答案 0 :(得分:0)
在点击功能中你可以做两件事之一,所以转到另一页,比如重定向到新页面
//click on a username to see that user's timeline.
$('a').on('click', function() {
//console.log("a tag is clicked ");
//console.log(this);
window.location = '/some_file.php?get_tweets='+tweet.user;
});
或者,使用ajax调用来执行相同的操作:
//click on a username to see that user's timeline.
$('a').on('click', function() {
//console.log("a tag is clicked ");
//console.log(this);
$.ajax({
type: "GET",
url: "/some_file.php",
data: {
'user' : tweet.user
},
success: function(msg) {
$body.html(msg);
});
});
在这两种情况下,some_file.php
都应格式化内容。