jQuery查找仅返回一个元素(在Meteor中) [英] jquery find returning only one element (in Meteor)
问题描述
我具有以下链接,这些链接在单击时应显示fancyboxes-在此处使用jquery插件: http://fancyapps .com/fancybox/
I have the following links that when clicked on should show fancyboxes--using the jquery plugin here: http://fancyapps.com/fancybox/
<div class="boxes">
<a href="/signup.html" class="btn popup-link fancybox.ajax">Sign Up</a>
<a href="/signin.html" class="btn popup-link fancybox.ajax">Sign In</a>
</div>
我遇到的问题是fancybox仅适用于第一个链接. 在流星中,我有:
The problem i'm having is that the fancybox works only for the first link. In Meteor, I have:
Template.mytemplate.rendered = function () {
console.log($(this.find('a.popup-link')));
$(this.find('a.popup-link')).fancybox({
padding: 18,
openMethod: 'changeIn',
closeBtn : false,
beforeShow: function() {
$('input:checkbox').ezMark();
$('select').selectbox();
$('.trigger-ajax').on('click', function(e) {
e.preventDefault()
$('.fancybox-wrap').animate({ 'left': '-100%'}, 400, function() {
$(this).parent().find('.popup-link').trigger('click');
})
})
}
});
}
当我单击第一个链接时,我的fancybox会按预期加载,但是当我单击第二个链接时,没有任何反应.
When i click on the first link my fancybox loads as expected but when i click on the second nothing happens.
通过console.log也可以清楚地发现,查找结果仅返回第一个元素……因此出现了问题.
Via the console.log its also clear that the find is returning only the first element ...hence the problem.
请注意,如果我只是在没有find
的情况下执行('a.popup-link')
,则它将不起作用.
Note it doesn't work if i just do ('a.popup-link')
without the find
.
这是怎么回事?谢谢!
推荐答案
如果您尝试使用Meteor模板方法(而不是jQuery find()),请使用this.findAll('a.popup-link')
代替this.find('a.popup-link')
方法).
Use this.findAll('a.popup-link')
instead of this.find('a.popup-link')
if you were trying to use the Meteor template method (not the jQuery find() method).
但是, findAll()返回一个DOM元素数组,我不确定$()是否会接受为参数.如果不是这样,则可以遍历数组的元素.
However, findAll() returns an array of DOM elements that I'm not sure if $() will accept as a parameter. If it doesn't though, you can just loop through the elements of the array.
这篇关于jQuery查找仅返回一个元素(在Meteor中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!