jQuery 中 error() 和 ajaxError() 事件有何區別?
jQuery error() 方法
當一個元素遭遇錯誤時,error() 方法將觸發事件。該方法在 jQuery 1.8 中已棄用。
示例
您可以嘗試執行以下程式碼,瞭解如何在 jQuery 中使用 error() 方法 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("img").error(function(){
$("img").replaceWith("<p>Image isn't loading</p>");
});
$("button").click(function(){
$("img").error();
});
});
</script>
</head>
<body>
<img src="/videotutorials/images/tutorial_library_home.jpg" alt="Video Tutorial" width="300" height="200"><br>
<button>Error event</button>
</body>
</html>jQuery ajaxError() 方法
ajaxError( callback ) 方法附加一個函式,在每次 AJAX 請求失敗時執行。這是一個 Ajax 事件。
示例
您可以嘗試執行以下程式碼,瞭解如何在 jQuery 中使用 ajaxError() 方法 −
<html>
<head>
<title>jQuery ajaxError() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
/* Assume result.text does not exist. */
$('#stage1').load('/jquery/result.text');
});
$(document).ajaxError(function(event, request, settings ){
$("#stage2").html("<h1>Error in loading page.</h1>");
});
});
</script>
</head>
<body>
<p>Click on the button to load result.text file:</p>
<div id = "stage1" style = "background-color:blue;">
STAGE - 1
</div>
<div id = "stage2" style = "background-color:blue;">
STAGE - 2
</div>
<input type = "button" id = "driver" value = "Load Data" />
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP