【发布时间】:2012-02-07 19:09:20
【问题描述】:
我有一些 PHP 方面的经验和一点 JS 方面的经验,但我还远远没有精通任何东西。我正在尝试为我的网站制作一个 jQuery 插件,我可以通过以下方式在我的 HTML 中调用它:
$('.new').tumble({username: "tylor", count: 9});
在这种情况下,这基本上会将代码应该制作的 Tumblr 列表放入带有“new”类的 DIV 中。到目前为止,这是我的代码;问题似乎是如何让它从原始调用(在 HTML 中)中获取类/ID 并在 jQuery 中使用它。
这是目前为止的代码:
(function($) {
$.fn.tumble = function(options){
var settings = $.extend({
username: null, // [string or array] required to get url for tumblr account
count: 3, // [integer] how many posts to display?
}, options);
//url construction
var url = "http://" + settings.username + ".tumblr.com";
var jsonurl = url + "/api/read/json?num=" + settings.count + "&callback=?";
$.getJSON(jsonurl, function(data) {
var items = [];
$.each(data.posts, function(id, url) { // Goes over each post in the JSON document retrieved from data URL
var url = this.url; // Just assigns a variable to the url to avoid constantly writing "this.whatever"
var photourl = this['photo-url-250']; // photo-url-xxx needs to be called this way due to integers in the name
items.push('<li><a href="' + url + '">' + photourl + '</a></li>');
});
$('<ul/>', { // Creates an empty list
html: items.join('') // Takes the values in the item array and puts 'em together
}).appendTo('.new'); // I don't want this to have the class set in the jQuery itself
}); //end json
};
})( jQuery );
您可以提供的任何帮助都会很棒。谢谢
【问题讨论】: