需要为插件的每个.post获取’a’名称
(function ($) { 'use strict'; var url = window.location.href.split('#')[0]; var post = $('.post').children('a[name]').attr('name'); var helpers = { "defaults": { "post": post, "href": url+'#', "send": 'true', "layout": 'button_count', "width": '125', "faces": 'false', "font": 'verdana', "action": 'like', "scheme": 'light', }, "init": function (options) { var settings = $.extend({}, helpers.defaults, options), easyface = $('').addClass('easyface fb-like').attr({ "data-href": settings.href + settings.post, "data-send": settings.send, "data-layout": settings.layout, "data-width": settings.width, "data-show-faces": settings.faces, "data-font": settings.font, "data-action": settings.action, "data-colorscheme": settings.scheme }); return this.each(function (i, elem) { var self = $(elem), data = self.data('easyface'); if (!data) { self.data('easyface', easyface); self.append(easyface); } }); }, "destroy": function () { return this.each(function (i, elem) { var self = $(this), data = self.data('easyface'); // test to see if we've already called init on this element $(window).unbind('.easyface'); // unbind any namespaced events, assuming you've namespaced them like "click.easyface" self.removeData('easyface'); // remove the data flag self.find('.easyface').remove(); // remove the appended div }); } }; //define the method "easyface" $.fn.easyface = function (method) { if (helpers[method]) { // call the method and pass in the settings return helpers[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof method === 'object' || !method) { // default to the init method and pass in the arg return helpers.init.apply(this, arguments); } else { // throw an error $.error('Method ' + method + ' does not exist on jQuery.tooltip'); } }; }(jQuery)); $(function() { $('body').append(''); (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=477049588983712"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); });
我认为问题出在这里
var url = window.location.href.split('#')[0]; var post = $('.post').children('a[name]').attr('name'); var helpers = {`
发生了什么事情就是想出这样的url –
http://www.easybbtutorials.com/t177-user-profile-home-page#1244#undefined
所以我添加了split('#')[0];
并且运作良好,但我仍然得到未定义的部分。
http://www.easybbtutorials.com/t177-user-profile-home-page#undefined
同样在每个.post
,这是每个fb的相同url,它需要为每个父母添加a
名字……
我可能会因为我说的而混淆,但我现在只是愤怒而且写得很快。
更好的解释:
- .post是第一个,fb data-href应该是
http://www.easybbtutorials.com/t177-user-profile-home-page#8870
- .post是第二个,fb data-href应该是
http://www.easybbtutorials.com/t177-user-profile-home-page#8871
- .post是第三个,fb data-href应该是
http://www.easybbtutorials.com/t177-user-profile-home-page#8872
- .post是第四个,fb data-href应该是
http://www.easybbtutorials.com/t177-user-profile-home-page#8873
所以等等,这是一个论坛,所以会有多个.post区域
截至目前,我必须像这样写电话……
$('.post').each(function() { $(this).easyface(); });
我希望插件能够自动执行任何操作。 所以$('.post').easyface()
会自动执行上面的代码。
此外,我现在需要它('a[name]').attr('name');
虽然我现在必须像这样添加它, ('a[name]').attr('name').split('p')[1];
因为所有的id都以p ex开头:p830 p338 p395 p代表post。 鉴于链接不会识别#p784所以它需要成为#784。
谁可以得到这个工作可以让我的所有声誉设置为100 …现在我只有43,但无论等待太长时间。
更好地理解这里::
http://jsfiddle.net/zUeFL/17/
正如你所看到的,我有四个喜欢和发送,就像他们都喜欢的那样。 每个post都需要不同的url。
问题1:
var post = $('.post').children('a[name]').attr('name');
children()
将返回元素列表。
问题2:
只需从$(document).ready
块中运行插件。
解
试试这个…
$(document).ready(function() { //run the plugin pNames = [] $('.post').children('a[name]').each(function() { pNames.push($(this).attr('name')); }); $('.postfoot').easyface({ "post": pNames }); });
更新
如果您只需要一个元素,那么执行以下操作:
var post = $('.post').children('a[name]')[0].attr('name');
创建href
时进行以下更改:
easyface = $('').addClass('easyface fb-like').attr({ "data-href": settings.href.substring(0, settings.href.length - 1) + "#" + settings.post.split("p")[1], // concatenate with your href here.
更新了jsFiddle: http : //jsfiddle.net/zUeFL/21/
我想这就是你要找的东西: demo
主要变化是:
-
postID是在
each()
循环中计算的,因为每个.postfoot
都会有所不同:post = (options && options.post) || self.closest('.post').find('a:first').attr('name');
这允许
options
对象覆盖postID,这可能不是一个好主意。 -
easyface
div也是在each()
循环中创建的,因为每个.postfoot
都会有所不同。