![](/img/trans.png)
[英]How do I take javascript identical functions and make then into a reusable function?
[英]How do I make this javascript reusable?
我有一些經常使用的代碼。 這是代碼和示例用法:
$('.close-cookie-notice').click(function (e) {
// make this reusable
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
// end of reusable code
$('#site-cookie-notice').slideUp();
});
我需要重用的代碼在注釋之間。 我希望這駐留在其自己的功能中,但不確定如何處理事件(e)的傳遞。
有人幫忙嗎?
function myspecialfunction(e) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
用作:
$('.close-cookie-notice').click(function (e) {
myspecialfunction(e);
$('#site-cookie-notice').slideUp();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.