gpt4 book ai didi

jquery - 需要获取插件的每个 .post 的 'a' 名称

转载 作者:行者123 更新时间:2023-12-01 07:20:09 24 4
gpt4 key购买 nike

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

我认为问题就出在这里

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

发生的事情是它出现了这样的网址 -

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

所以我添加了 split('#')[0]; 并且效果很好,但我仍然得到未定义的部分。

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

此外,在每个 .post 中,每个 Facebook 喜欢的 URL 都是相同的,它需要添加每个 parent a 名称 ...

我可能会感到困惑,但我现在很愤怒,写得很快。

更好的解释:

  1. .post 是第一个,fb data-href 应该是 http://www.easybbtutorials.com/t177-user-profile-home-page#8870
  2. .post 是第二个,fb data-href 应该是 http://www.easybbtutorials.com/t177-user-profile-home-page#8871
  3. .post 是第三个,fb data-href 应该是 http://www.easybbtutorials.com/t177-user-profile-home-page#8872
  4. .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站岗。并且给定的链接无法识别#p784,因此它需要变为#784。

谁能做到这一点,就可以拥有我设置为 100 的所有声望……现在我只有 43,但无论如何,等得太久了。

在这里更好地理解::

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

正如你所看到的,我有四个喜欢并发送,就像他们都喜欢的一样。每个帖子需要不同的网址。

最佳答案

问题 1:

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

children() 将返回元素列表。

问题 2:

只需从 $(document).ready block 内运行插件即可。

解决方案

试试这个...

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

pNames = []

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

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

更新

如果您只需要一个 a 元素,请执行以下操作:

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

创建 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.

更新了 jsFiddle: http://jsfiddle.net/zUeFL/21/

关于jquery - 需要获取插件的每个 .post 的 'a' 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14658850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com