JQuery - 获取(0)未定义 [英] JQuery - get(0) undefined

查看:76
本文介绍了JQuery - 获取(0)未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML结构:

 < div id =navigation> 
< div id =accordion>
<! - 第一个标题和窗格 - >
< img src =horisontalTabsAccordion / images / button1.jpg/>
< div style =width:200px; display:block>< h3> Apolonija,zubarska ordinacija< / h3>< p> Nesto pise malo vise malo manje:)< / p>< ; / DIV>

<! - 第二个标头和窗格 - >
< img src =horisontalTabsAccordion / images / button2.jpg/>
< div>< h3> Apolonija,zubarska ordinacija< / h3>< p> Nesto pise malo vise malo manje:)< / p>< / div>

<! - 第3个标题和窗格 - >
< img src =horisontalTabsAccordion / images / button3.jpg/>
< div>< h3> Apolonija,zubarska ordinacija< / h3>< p> Nesto pise malo vise malo manje:)< / p>< / div>

<! - 第4个标题和窗格 - >
< img src =horisontalTabsAccordion / images / button4.jpg/>
< div>< h3> Apolonija,zubarska ordinacija< / h3>< p> Nesto pise malo vise malo manje:)< / p>< / div>
< / div>
< div id =buttons>
< a href =#class =btn-prev>< div class =previous-icon>< / div>< / a>
< a href =#class =btn-next>< div class =next-icon>< / div>< / a>
< / div>
< / div>

我正在尝试添加按钮以激活当前选项卡的下一个和前一个选项卡。图片元素是当前有类当前。

我写的JS有这样的形式:

<$ p $($#$> $($)$(document).ready(function(e){
var currentTab = $('#accordion')。find('img.current');

$('。btn-prev')。bind('click',function(){
currentTab.prev('img')。get(0).click();
console .log(Previous);

return false;
});
$ b $('。btn-next')。bind('click', function(){
currentTab.next('img')。get(0).click();
console.log(Next);

return false;
});

});

我有错误,说是get(0)undefined。 - TypeError:currentTab.prev(img)。get(0)is undefined

解决方案

prev 获取之前的元素 IF 它匹配选择器,而不是前一个元素 THAT 匹配选择器。



试试这个:

  currentTab.prevUntil('img')。prev()。get(0) 

.next()需要相同治疗 .nextUntil



您还应该进行一些检查,以防止您在第一次时出现错误一个。

I have this HTML structure:

<div id="navigation">
    <div id="accordion">
        <!-- 1st header and pane -->
        <img src="horisontalTabsAccordion/images/button1.jpg" />
        <div style="width:200px; display:block"><h3>Apolonija, zubarska ordinacija</h3><p>Nesto pise malo vise malo manje :)</p></div>

        <!-- 2nd header and pane -->
        <img src="horisontalTabsAccordion/images/button2.jpg" />
        <div><h3>Apolonija, zubarska ordinacija</h3><p>Nesto pise malo vise malo manje :)</p></div>

        <!-- 3rd header and pane -->
        <img src="horisontalTabsAccordion/images/button3.jpg" />
        <div><h3>Apolonija, zubarska ordinacija</h3><p>Nesto pise malo vise malo manje :)</p></div>

        <!-- 4th header and pane -->
        <img src="horisontalTabsAccordion/images/button4.jpg" />
        <div><h3>Apolonija, zubarska ordinacija</h3><p>Nesto pise malo vise malo manje :)</p></div>
    </div>
    <div id="buttons">
        <a href="#" class="btn-prev"><div class="previous-icon"></div></a>
        <a href="#" class="btn-next"><div class="next-icon"></div></a>
    </div>
</div>

I'm trying to add buttons for activation next and previous tabs of current tab. Image element that is current have class "current".

The JS that i wrote have this form:

$(document).ready(function(e) {
var currentTab = $('#accordion').find('img.current');

$('.btn-prev').bind('click', function(){
    currentTab.prev('img').get(0).click();
    console.log("Previous");

    return false;
});

 $('.btn-next').bind('click', function(){
    currentTab.next('img').get(0).click();
    console.log("Next");

    return false;
});

});

I have error that says that is get(0) undefined. - TypeError: currentTab.prev("img").get(0) is undefined

解决方案

.prev gets the previous element IF it matches the selector, not the previous element THAT matches the selector.

Try this:

currentTab.prevUntil('img').prev().get(0)

.next() will need the same treatment with .nextUntil

You should also do some checking to prevent errors from pressing previous when you are on the first one.

这篇关于JQuery - 获取(0)未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆