jQuery擴展方法


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>jQuery擴展方法(調用自己寫的方法)</title>
<meta charset="UTF-8">
</head>
<body>
<div>hello div</div>
<script src="jquery-3.4.1.js"></script>
<script>
//1.$.extend(),通過 $ 調用
// $.extend({
// getMax:function (a,b) {
// return a>b?a:b;
//
// }
// });
// alert($.getMax(6,4));
//2.$.fn.extend() ,$ 加 標簽名 調用
// $.fn.extend({
// print:function () {
// console.log($(this).html())
// }
// });
// $('div').print();
//=======================================
// (function (a) {
// alert(a)
//
// })('come up');//come up
//以后寫擴展方法 要將擴展方法放到一個匿名函數里面去
//私有域:避免與其他變量產生沖突,與外部實現解耦
(function () {
$.fn.extend({
print:function () {
console.log($(this).html())
}
});
})()
$('div').print();
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM