小白教程
所有教程
关于
Search
172.70.100.99
172.70.100.99
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Event error”的源代码
本页内容
上一节:
Event_die
下一节:
Jq_event_currenttarget
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:error()}}[[Category:jquery ref events|8]] = jQuery error() 方法 = [[jquery ref events|jQuery 事件方法]] 如果 image 元素遇到错误,把它替换为文本: <sample title="" desc="" lang="html" hererun="1"> $("img").error(function(){ $("img").replaceWith("<p>图片加载错误!</p>"); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://apps.bdimg.com/libs/jquery/1.7.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("img").error(function(){ $("img").replaceWith("<p>图片加载错误!</p>"); }); $("button").click(function(){ $("img").error(); }); }); </script> </head> <body> <img src="img_pulpitrock.jpg" alt="Pulpit rock" width="284" height="213"> <br> <button>图片触发 error 事件</button> </body> </html> </run> == 定义和用法 == error() 方法在 jQuery 版本 1.8 中[[NewWindow('deprecated.htm')|被废弃]]。 当元素遇到错误时(当元素没有正确载入时),发生 error 事件。 error() 方法触发 error 事件,或规定当发生 error 事件时运行的函数。 '''提示:'''该方法是 bind('error', handler) 的简写方式。 == 语法 == 触发被选元素的 error 事件: $(selector).error() <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://apps.bdimg.com/libs/jquery/1.7.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("img").error(function(){ $("img").replaceWith("<p>图片加载错误!</p>"); }); $("button").click(function(){ $("img").error(); }); }); </script> </head> <body> <img src="img_pulpitrock.jpg" alt="Pulpit rock" width="284" height="213"> <br> <button>图片触发 error 事件</button> </body> </html> </run> 添加函数到 error 事件: $(selector).error(function) <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://apps.bdimg.com/libs/jquery/1.7.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("img").error(function(){ $("img").replaceWith("<p>图片加载错误!</p>"); }); }); </script> </head> <body> <img src="errorimg.gif"> <p>如果图片加载错误,将被替换指定文本。</p> </body> </html> </run> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | function | 可选。规定当 error 事件发生时运行的函数。 |} [[jquery ref events|jQuery 事件方法]]
返回至“
Event error
”。
上一节:
Event_die
下一节:
Jq_event_currenttarget