为什么这个点击处理程序fadeToggle代码不起作用?

时间:2011-12-15 02:36:11

标签: php javascript jquery

我试图淡化div元素但由于某种原因,每次我预览我的脚本代码时它只显示它并且不运行它

我的脚本代码是 -

 <script>
$("#t<?php echo $pNumber ?>").click(function() {
$("#b.<?php echo $pNumber ?>").fadeToggle("slow", "linear");
});
</script>

PHP变量用于更改div id会导致问题吗?

谢谢,

更新 -

示例html这是针对其中一个div(id递增)

  <div id="t1">Carl Froch 'fitter than ever' for Andre Ward Showtime Super Six       fight</div>
  <div id="b1"><p>Carl Froch gives Andre Ward seven years but no encouragement to      believe their fight  on Saturday night is a war of the ages.</p><p>The 34-year-old WBC    champion from Nottingham rarely strays far from the 12-stone limit of his division and is     hard o...<p><a href=http://www.guardian.co.uk/sport/2011/dec/14/carl-froch-andre-  ward-showtime-super-six>Click here for full article</a></p>   </div  <script>
  $("#t1").click(function() {
  $("#b1").fadeToggle("slow", "linear");
  });
 </script>

JB

1 个答案:

答案 0 :(得分:2)

在元素选择器中:

  • 您将$pNumber作为点击事件的t id的一部分加入
  • 您将$pNumber作为b class 的一部分加入fadeToggle

要解决此问题,请执行以下操作之一:

  • #t选择器上添加一个点,使其根据class
  • 进行选择
  • #b选择器中移除点,使其根据id
  • 进行选择

请参阅this fiddle before removing the .

$("#t1").click(function() {
    $("#b.1").fadeToggle("slow", "linear");
});

请参阅this fixed fiddle

$("#t1").click(function() {
    $("#b1").fadeToggle("slow", "linear");
});