jquery find returning only one element (in Meteor)

六月ゝ 毕业季﹏ 提交于 2019-12-24 12:40:14

问题


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>

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

}

When i click on the first link my fancybox loads as expected but when i click on the second nothing happens.

Via the console.log its also clear that the find is returning only the first element ...hence the problem.

Note it doesn't work if i just do ('a.popup-link') without the find.

What's going on? Thanks!


回答1:


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).

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.




回答2:


I think you're using a different find method. Use:

$(this).find('a.popup-link').fancybox({

I'm not sure how this.find('a.popup-link') was ever working, but I guess it depends on what this and find were/did. To use the jQuery method find, you need a jQuery object. That's what $(this) does. From there, you can find "a.popup-link" descendants with find.



来源:https://stackoverflow.com/questions/15885287/jquery-find-returning-only-one-element-in-meteor

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