在加载 HTML 对象时,在通过正确的方法处理 Chrome 中的加载事件时遇到了一些麻烦。
我正在使用 HTML 对象将小部件加载到基于 Web 的仪表板中,加载事件在 Chrome 中看起来被破坏了,因为我重复触发了加载事件,但前提是我在加载的对象上设置了样式事件定位:绝对。不幸的是,对于我的应用程序来说,这是一个关键功能,因为我需要使用 javascript 将小部件放置在特定的屏幕位置。
下面的 fiddle 将使 Chrome 进入无限循环,而它在其他浏览器中只触发一次(尽管在我的应用程序中 Chrome 触发加载事件两次然后停止)。
HTML
<div id="container"></div>
<br/><br/><br/><br/>
<div id="xx">Not Fired..."</div>
JavaScript:
var cnt = 0;
(function loadWidget() {
var widgetObj = document.createElement("object");
widgetObj.data = ("http://13.75.145.9/widgets/dial.html") // location of widget
var tt = document.getElementById("container");
tt.appendChild(widgetObj);
widgetObj.addEventListener("load", widgetLoaded, false);
})();
function widgetLoaded(e) {
cnt = cnt + 1;
document.getElementById("xx").innerText = "fired x" + cnt;
//any manipulation of style is OK, its only the position: absolute that causes re-firing
e.currentTarget.style = "cursor: move; text-decoration: overline";
e.currentTarget.style.setProperty("position", "relative");
// Uncomment out the line below in Chrome and the event will continuously fire, but only once in othe browsers
e.currentTarget.style = "position: absolute; left: 50px";
}
我假设这是一个 Chrome 错误,虽然我可以通过检查对象已加载的次数并提前退出加载事件来解决它,但会降低性能,因为它看起来整个对象都是每次都重新加载(重新运行对象中的全局 JavaScript),并且在将几个小部件作为对象加载时,浏览器会显着降低速度,从而影响最终用户体验。
无论我尝试什么,我似乎都无法避免多次重新加载的发生。有没有其他人看到这个错误并且知道如何避免重新加载?
在相关说明中,Chrome 将呈现并显示对象,然后将其移动到正确的位置,这会闪烁屏幕,而其他浏览器将在其最终位置呈现小部件(在运行加载 javascript 之后),这是一个更好的体验,我找不到停止 Chrome 渲染两次的方法。
[18 年 12 月更新] - 以下答案中概述的解决方法不再适用于最新版本的 Chrome (71.0.3578.80),并且 Chrome 会双重加载 HTML 对象并双重触发加载的事件。有关详细信息,请参阅此 Chromium 错误帖子:Chrome bug with HTML Objects
最佳答案
不确定 Chrome 中出现此类行为的原因。但是,解决方法似乎是更改容器的样式而不是对象的样式 - see fiddle
function widgetLoaded(e) {
const target = e.currentTarget;
target.parentElement.style = "position: absolute; left: 50px;";
}
同样经过一些调查,我发现问题可能是由于将 display 从 inline 更改为 block - see fiddle .
关于javascript - 设置样式位置 : absolute 时,Chrome 对象标签加载多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019134/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我有一大串格式化数据(例如JSON),我想使用Psychinruby同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss