javascript
onload事件_图像onLoad事件+ Internet ExplorerJavaScript问题
onload事件
I was recently coding an application that would inject an image into the page and then execute a given function when the image's onLoad event fires. My code was working everywhere except Internet Explorer. That wasn't all together shocking initially but the fact that even IE8 was failing to the fire the onLoad event was discouraging. Here's what my code looked like:
我最近正在編寫一個(gè)應(yīng)用程序,該應(yīng)用程序?qū)D像插入頁面,然后在圖像的onLoad事件觸發(fā)時(shí)執(zhí)行給定的功能。 我的代碼在Internet Explorer 以外的所有地方都可以使用 。 最初這并沒有使所有人震驚,但是即使IE8也無法觸發(fā)onLoad事件,這一事實(shí)令人沮喪。 這是我的代碼:
var img = new Element('img',{alt: this.title ? this.title.get('html') : this.options.url,src: this.options.url,events: {error: function() {this.messageBox.set('html',this.options.errorMessage);img.dispose();}.bind(this),load: function() {img.setStyle('display','');this.unfade();if(!this.footer) {img.setStyle('cursor','pointer').addEvent('click',this.close.bind(this));}}.bind(this)},styles: {display: 'none'} }).inject(this.messageBox);On a hunch I detached the "src" assignment and coded that as a separate statement:
憑直覺,我分??離了“ src”賦值并將其編碼為單獨(dú)的語句:
var img = new Element('img',{alt: this.title ? this.title.get('html') : this.options.url,events: {error: function() {this.messageBox.set('html',this.options.errorMessage);img.dispose();}.bind(this),load: function() {img.setStyle('display','');this.unfade();if(!this.footer) {img.setStyle('cursor','pointer').addEvent('click',this.close.bind(this));}}.bind(this)},styles: {display: 'none'} }); img.set('src',this.options.url).inject(this.messageBox); //for ieNot too surprisingly that worked. The reason my modification worked is that image was being pulled from cache as soon as the SRC attribute was set, thus "beating" the event assignment to the punch. Keep this in mind if you run into onLoad issues with your images.
這樣做并不奇怪。 我的修改起作用的原因是,一旦設(shè)置了SRC屬性,就會(huì)從緩存中拉出圖像,從而將事件分配“打”到打Kong器。 如果圖像遇到onLoad問題,請(qǐng)記住這一點(diǎn)。
翻譯自: https://davidwalsh.name/image-load-event
onload事件
總結(jié)
以上是生活随笔為你收集整理的onload事件_图像onLoad事件+ Internet ExplorerJavaScript问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MapGIS基础开发环境搭建(IGSse
- 下一篇: 2#使用新安装的ubuntu,之vim必