Love beautiful code? We do too.
Khi có lỗi xảy ra do một thành phần gặp lỗi có thể là do thành phần được tải không chính xác, khi đó .error() sẽ kích hoạt sự kiện lỗi hoặc thực hiện một function.
Đã được thêm vào từ phiên bản 1.0
.error()
$('img').error();
Đã được thêm vào từ phiên bản 1.4.3
.error(function(){...})
$('img').error(function() {
$('img').replaceWith('<p>Error loading image!</p>');
});
Html viết:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('img').error(function(){
$('img').replaceWith('<p>Error loading image!</p>');
});
$('button').click(function(){
$('img').error();
});
});
</script>
</head>
<body>
<img src="https://hoclaptrinh.vn/themes/client/img/code-screen.jpg" alt="Học Web Chuẩn" /><br />
<button>Click</button>
</body>
</html>
Hiển thị trình duyệt:
Ta thấy khi click button thì sự kiện lỗi đã được gọi
Hoclaptrinh.vn © 2017
From Coder With
Unpublished comment
Viết câu trả lời