2015-01-05 27 views
0

所以我跟着这个视频写得更聪明,但我的代码不像他的工作。所以我的结论是,我的jQuery似乎没有找到panelid,我可以提醒panelid和那个工程。我的功能是点击“läsmer”,一个文本应该弹出,并且我更改mindre - 。我从哪里出发?!我很快就会疯了。我的jquery似乎没有找到面板

的YT视频:https://www.youtube.com/watch?v=Cc3K2jDdKTo

HTML:

<ul> 
    <li> <a data-panelid="panel1" class="showhide" href="#">Läs mer +</a> </li> 
    <li> <a data-panelid="panel2" class="showhide" href="#">Läs mer +</a> </li> 
    <li> <a data-panelid="panel3" class="showhide" href="#">Läs mer +</a> </li> 
</ul> 
<div id="panel1" class="textstyle">Ett enda bett från inlandstaipanen innehåller tillräckligt mycket gift för att döda upp till 100 vuxna människor eller 250 000 möss.</div> 
<div id="panel2" class="textstyle">ouahsfouashfoas aushfaslal.</div> 
<div id="panel3" class="textstyle">ioajsfoiasjgoiashfoas oahsgkmaspof.</div> 

的Jquery:

$(document).ready(function() { 
    $(".showhide").click(function() { 
     var panelId = $(this).attr('data-panelid') 
     alert(panelId); 
     var txt = $(this).text(); 
     $(panelId).toggle(); 
     if (txt == "Läs mer +") { 
      $(this).text('Mindre -'); 
     } else { 
      $(this).text('Läs mer +'); 
     } 
    }); 
}); 

感谢你所做的一切:)

回答

4

您需要使用ID Selector (“#id”),所以加#panelId

$('#' + panelId) 

如果你的变量值panel1然后$(panelId)将寻找不存在的,因此元素panel1它不工作

+0

嗨,老兄,感谢这么多的帮助!但它仍然没有工作,我认为问题是,镶板无法找到div id对不起,我是新来的。 – CodingNoob

+0

@CodingNoob:这应该工作,如果你有正确的panelid。尝试使用数据方法'var panelId = $(this).data('panelid')'获取面板ID' –

+0

@CodingNoob,您的代码工作见http://jsfiddle.net/89my6sxa/ – Satpal

1

这里是jsfiddle链接,你可以看到它的工作。

http://jsfiddle.net/g4xd3u05/

$(document).ready(function() { 
    $(".showhide").click(function() { 
     var panelId = $(this).attr('data-panelid') 
     var txt = $(this).text(); 
     $('#' + panelId).toggle(); //changed it here from just panelId 
     if (txt == "Läs mer +") { 
      $(this).text('Mindre -'); 
     } else { 
      $(this).text('Läs mer +'); 
     } 
    }); 
});