最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why am I getting this error "block is not defined"? - Stack Overflow

programmeradmin1浏览0评论

I am getting this javascript error: "block is not defined"

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

    $(".fp_top_right_login").slideToggle(600);
    var st = document.getElementById("fp_top_right_register").style.display;
    if(st == "" || st == "none")
    {
      window.setTimeout(document.getElementById("fp_top_right_register").style.display="block",600); //error happens here
    }
    else
    {
      window.setTimeout(document.getElementById("fp_top_right_register").style.display="none",600); //and also here
    }
  });
});
</script>

I am getting this javascript error: "block is not defined"

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

    $(".fp_top_right_login").slideToggle(600);
    var st = document.getElementById("fp_top_right_register").style.display;
    if(st == "" || st == "none")
    {
      window.setTimeout(document.getElementById("fp_top_right_register").style.display="block",600); //error happens here
    }
    else
    {
      window.setTimeout(document.getElementById("fp_top_right_register").style.display="none",600); //and also here
    }
  });
});
</script>
Share Improve this question asked Sep 15, 2011 at 13:49 AllisonCAllisonC 3,1004 gold badges31 silver badges49 bronze badges 4
  • 1 Can you make a JsFiddle with a testcase of the problem? – Exelian Commented Sep 15, 2011 at 13:51
  • So no one else has yet asked why you're using "getElementById" right next to a jQuery selector? Use $('#foo') instead! – Adam Terlson Commented Sep 15, 2011 at 14:10
  • Why are you using document.getElementById("fp_top_right_register").style.display="block" when you're using jQuery anyway? $("#fp_top_right_register").show() isn't any faster, but it's a lot shorter and easier to read. – Blazemonger Commented Sep 15, 2011 at 14:14
  • This code wasn't written by me, I'm just fixing things on our site – AllisonC Commented Sep 15, 2011 at 15:07
Add a ment  | 

3 Answers 3

Reset to default 7

setTimeout takes a function as a parameter. You can use an anonymous function. Example:

window.setTimeout(function() {
    document.getElementById("fp_top_right_register").style.display="block"; 
}, 600);

There is an error in you way of using setTimeout.

window.setTimeout(function(){document.getElementById("fp_top_right_register").style.display="block"},600);

Also, consider using jquery css :

$('#fp_top_right_register').css('display','block');

The first argument to window.setTimeout should be a function, not a string, which is the result of your assignment.

You probably wanted to wrap that assignment in

function () { .... }
发布评论

评论列表(0)

  1. 暂无评论