利用闭包重写document.createElement函数

本文介绍了一种使用JavaScript扩展原生document.createElement方法的新技巧,通过传递类型、ID及类名来简化DOM元素的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

document.createElement = (function(fn){
    return function(type,id,className){
    var elem = fn.call(document,type);
    if(id) elem.id = id;
    if(className) elem.className = className;
    return elem;
}
})(document.createElement);

var t = document.createElement("div","abc","ddd");
 
``` // 更新分页控件 function updatePagination(totalPages, currentPage) { const paginationContainer = document.getElementById('pagination'); paginationContainer.innerHTML = ''; if (currentPage > 1) { const prevBtn = document.createElement('button'); prevBtn.textContent = '上一页'; prevBtn.onclick = () => { currentPage--; loadData(); }; paginationContainer.appendChild(prevBtn); } let startPage, endPage; if (totalPages <= 5) { startPage = 1; endPage = totalPages; } else { startPage = Math.max(1, currentPage - 2) endPage = Math.min(totalPages, currentPage + 2); } if (startPage > 1) { const firstPageBtn = document.createElement('button'); firstPageBtn.textContent = '1'; firstPageBtn.onclick = () => { currentPage = 1; loadData(); }; paginationContainer.appendChild(firstPageBtn); if (startPage > 2) { const ellipsisStart = document.createElement('span'); ellipsisStart.textContent = '...'; paginationContainer.appendChild(ellipsisStart); } } for (let i = startPage; i <= endPage; i++) { const pageBtn = document.createElement('button'); pageBtn.textContent = i; if (i === currentPage) { pageBtn.style.backgroundColor = '#ddd'; } pageBtn.onclick = () => { currentPage = i; loadData(); }; paginationContainer.appendChild(pageBtn); } if (endPage < totalPages) { if (endPage < totalPages - 1) { const ellipsisEnd = document.createElement('span'); ellipsisEnd.textContent = '...'; paginationContainer.appendChild(ellipsisEnd); } const lastPageBtn = document.createElement('button'); lastPageBtn.textContent = totalPages; lastPageBtn.onclick = () => { currentPage = totalPages; loadData(); }; paginationContainer.appendChild(lastPageBtn); } if (currentPage < totalPages) { const nextBtn = document.createElement('button'); nextBtn.textContent = '下一页'; nextBtn.onclick = () => { currentPage++; loadData(); }; paginationContainer.appendChild(nextBtn); } // 添加跳转输入框 const gotoInput = document.createElement('input'); gotoInput.type = 'number'; gotoInput.min = '1'; gotoInput.max = totalPages; gotoInput.placeholder = '跳转到...'; gotoInput.style.marginLeft = '10px'; gotoInput.style.width = '50px'; paginationContainer.appendChild(gotoInput); const gotoBtn = document.createElement('button'); gotoBtn.textContent = '跳转'; gotoBtn.onclick = () => { const page = parseInt(gotoInput.value, 10); if (page >= 1 && page <= totalPages) { currentPage = page; loadData(); } }; paginationContainer.appendChild(gotoBtn); }```如果运行该段代码后的返回值为undefined,如何处理?
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值