简体   繁体   English

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

[英]Why isn't this click handler fadeToggle code working?

I am trying to fade in div elements but for some reason every time I preview my script code it just displays it and doesn't run it 我试图淡入div元素但由于某种原因每次我预览我的脚本代码它只显示它并且不运行它

my script code is - 我的脚本代码是 -

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

the PHP variable is used for changing div id's would this be causing a problem? PHP变量用于更改div id会导致问题吗?

Thanks, 谢谢,

UPDATE - 更新 -

example html this is for one of the div's (the id is incremented) 示例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 JB

In your element selectors: 在元素选择器中:

  • You're including the $pNumber as part of the t 's id for the click event 你将$pNumber包含在click事件的t id
  • You're including the $pNumber as part of the b 's class for the fadeToggle 你将$pNumber包含在fadeToggleb class

To fix this, do one of these: 要解决此问题,请执行以下操作之一:

  • Add a dot to the #t selector, to make it select based on class #t选择器中添加一个点,使其根据class选择
  • Remove the dot from the #b selector, to make it select based on id #b选择器中删除点,使其根据id进行选择

See this fiddle before removing the . 在删除之前看到这个小提琴. :

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

See this fixed fiddle : 看到这个固定的小提琴

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM