need to get the 'a' name for each .post for the plugin

為{幸葍}努か 提交于 2019-12-11 05:09:25

问题


(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 = $('<div />').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('<div id="fb-root"></div>');

    (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'));
});

I think the issue lies here

var url = window.location.href.split('#')[0];
var post = $('.post').children('a[name]').attr('name');
var helpers = {`

Whats happening is it is coming up with the url like this -

http://www.easybbtutorials.com/t177-user-profile-home-page#1244#undefined

so I added the split('#')[0]; and that worked well but I am still getting the undefined part.

http://www.easybbtutorials.com/t177-user-profile-home-page#undefined

Also in each of the .post where this is it is the same url for each of the fb likes it needs to add each parents a name ...

I may come off confusing how I said it but I'm just furious right now and writing fast.

Better explaination:

  1. .post is the first one and the fb data-href should be http://www.easybbtutorials.com/t177-user-profile-home-page#8870
  2. .post is the second one and the fb data-href should be http://www.easybbtutorials.com/t177-user-profile-home-page#8871
  3. .post is the third one and the fb data-href should be http://www.easybbtutorials.com/t177-user-profile-home-page#8872
  4. .post is the fourth one and the fb data-href should be http://www.easybbtutorials.com/t177-user-profile-home-page#8873

So on and so forth, this is a forum so there will be multiple .post areas

As of now I have to write the call like so...

$('.post').each(function() {
   $(this).easyface();
  });

I want the plugin to automatically do each anything really. so $('.post').easyface() will automatically do the code like above.

Also I need it to as of now get the ('a[name]').attr('name'); of each post as well, though I've had to add it like this now, ('a[name]').attr('name').split('p')[1]; because all the id's start with p ex: p830 p338 p395 p standing for post. And given links won't recognize #p784 so it needs to become #784.

Who ever can get this working can have all of my reputation that is set at 100... now I only have 43, but whatever waited too long for this.

BETTER UNDERSTANDING HERE::

http://jsfiddle.net/zUeFL/17/

As you can see I have four likes and send, like one they all like. Need a different url for each post.


回答1:


Issue 1:

var post = $('.post').children('a[name]').attr('name'); 

children() will return a list of elements.

Issue 2:

Just run the plugin from inside a $(document).ready block.

Solution

Try this...

$(document).ready(function() {
    //run the plugin

    pNames = []

    $('.post').children('a[name]').each(function() {
        pNames.push($(this).attr('name'));
    });

    $('.postfoot').easyface({
        "post": pNames
    });
});

Update

If you only need one a element, then do this:

var post = $('.post').children('a[name]')[0].attr('name'); 

Make these changes when creating href:

easyface = $('<div />').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.

Updated jsFiddle: http://jsfiddle.net/zUeFL/21/




回答2:


I think this is what you're looking for: demo

The main changes are:

  • The post ID is computed within the each() loop, since it will be different for each .postfoot:

    post = (options && options.post) || self.closest('.post').find('a:first').attr('name');
    

    This allows the post ID to be overridden by the options object, which may not be a good idea.

  • The easyface div is also created within the each() loop, again because it will be different for each .postfoot.



来源:https://stackoverflow.com/questions/14658850/need-to-get-the-a-name-for-each-post-for-the-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!