jquery .attr()只在chrome中工作?

时间:2011-04-25 19:48:21

标签: jquery jquery-selectors

有人知道为什么这段代码只适用于chrome吗?我想添加多个滑块,我开始尝试根据它们的ID来操作它们!

<html>
<head>

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" >



</script>
<script type="text/javascript"> 
$(document).ready(function(){
$(".flip").click(function(){

    var currentId = $(this).attr('id');
    panel = "#panel"+currentId;

    $(panel).slideToggle("slow");

  });
});
</script>

<style type="text/css"> 
div.panel,p.flip
{
    margin:0px;
    padding:5px;
    text-align:center;
    background:#e5eecc;
     border:solid 1px #c3c3c3;
}
div.panel
{
    height:120px;
    display:none;
}
</style>
</head>

<body>


<div class="panel" id="panel1">
<p>Because time is valuable, we deliver quick and easy learning.</p>
<p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p>
</div>

<p class="flip" id="1">Show/Hide Panel</p>


<div class="panel" id="panel2" >
<p>Because time is valuable, we deliver quick and easy learning.</p>
<p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p>
</div>

<p class="flip" id="2">Show/Hide Panel</p>


</body>
</html>

2 个答案:

答案 0 :(得分:1)

在这里,我甚至不会在寻找基于向ID添加数字的DIV时,它似乎是一种过于复杂的做事方式为什么不只是嵌套要在点击翻转时显示的面板< / p>

http://jsfiddle.net/samccone/b6RkH/3/

您的代码在我使用最新版本的jquery

测试的所有浏览器中都有效

答案 1 :(得分:0)

<script type="text/javascript"> 
$(document).ready(function(){
$(".flip").bind('click',function(){
    var currentId = $(this).attr('id');
    var panel = "#panel"+currentId;
   $(panel).slideToggle("slow");
   });
});
</script>