在 JavaScript 中,如果你已经通过 hook 修改了 getAttribute
方法,你可以通过保存原始方法的引用来调用原生的 getAttribute
。以下是一个示例:
// 保存原生的 getAttribute 方法
const originalGetAttribute = Element.prototype.getAttribute;
// Hook getAttribute 方法
Element.prototype.getAttribute = function (name) {
console.log(`Hooked getAttribute called with name: ${name}`);
// 这里可以添加你自定义的逻辑
// 调用原生的 getAttribute 方法
return originalGetAttribute.call(this, name);
};
// 使用示例
const element = document.createElement('div');
element.setAttribute('data-example', 'value');
console.log(element.getAttribute('data-example')); // 输出: "value" 并打印日志
在这个示例中,首先保存了 Element.prototype.getAttribute
的引用到 originalGetAttribute
变量。然后重写了 getAttribute
方法,在新方法中,可以执行一些自定义操作后,通过调用保存的原始方法来获得真实值。这种方式确保了不破坏现有的功能。
内容由零声教学AI助手提供,问题来源于学员提问