草庐IT

javascript - IE 引发无效参数错误,firefox 没有

coder 2025-01-19 原文

当我想炫耀 JavaScript 的强大功能时,我正在帮助一个 friend 学习 HTML、CSS 等。我们制作了一些包含云图像的 DIV,然后将它们移动到整个浏览器(从右到左,然后当它离开页面时它返回到窗口 innerWidth 的起点)所以它看起来像天空 - 非常简单脚本。现在在 Firefox 中一切正常,但稍后在 IE 中查看时,我注意到一个错误。这不是真正的问题,因为脚本可以正常工作,但我想知道它为什么会发生以及如何阻止它!

这是我在 window.onload 事件上使用不显眼的加载器调用的脚本

    function moveCloud(cloudID, TimeOut, CloudWidth, thisLeft){

    var elem = document.getElementById(cloudID);
    var xpos = parseInt(elem.style.left);

    if(xpos > -CloudWidth){
        xpos--; 
      }else{
        xpos = window.innerWidth;
       }

    elem.style.left = xpos + "px";
    movement = setTimeout("moveCloud('" + cloudID + "'," + TimeOut + "," + CloudWidth + "," + thisLeft+ ")",TimeOut);
}

我认为这可能是由于将一个整数连接到一个字符串,但我认为 JavaScript 处理了这个转换?有什么想法吗?

这是错误

Line: 38 Error: Invalid argument.

*更新***

这就是我调用函数的方式,首先是这个

function StartCloud(cloudID, TimeOut, CloudWidth, thisLeft, thisTop){


    var elem = document.getElementById(cloudID); 
    if(elem){ // make sure item exists then position it via the JavaScript not the CSS
        elem.style.position = "absolute"; 
        elem.style.left = parseInt(thisLeft) + "px";
        elem.style.top = parseInt(thisTop)  + "px";
        movement = setTimeout("moveCloud('" + cloudID + "'," + TimeOut + "," + CloudWidth + "," + thisLeft+ ")",TimeOut); // start the animation function
    }
}

这是我的决定 - 有 4 朵云 DIVS

window.onload = function (){
    //unobtrussive event handler
    StartCloud('cloud1', 60, 717, 450, 30);
    StartCloud('cloud2', 35, 349, 950, 230);
    StartCloud('cloud3', 30, 335, 300, 260);
    StartCloud('cloud4', 15, 290, 600, 450);
}

最佳答案

呃! IE 不支持 Window.innerWidth,所以我不得不使用 document.documentElement.clientWidth 编写条件

关于javascript - IE 引发无效参数错误,firefox 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5686452/

有关javascript - IE 引发无效参数错误,firefox 没有的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  6. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  7. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  8. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  9. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  10. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

随机推荐