我的知识记录

怎么修改网页源代码来修改数字(图文)

修改网页上的数字有多种方法,我将为您详细介绍各种技术和应用场景:

1. 使用浏览器开发者工具临时修改

方法一:直接编辑HTML元素


 
 
1. 按F12打开开发者工具 2. 选择Elements标签 3. 找到包含数字的HTML元素 4. 双击直接修改数字内容 5. 按Enter确认修改

方法二:使用Console控制台修改


 
javascript
// 修改特定元素中的数字 document.querySelector('#score').textContent = '95'; // 批量修改同类元素中的数字 document.querySelectorAll('.price').forEach(element => { element.textContent = '$' + (parseFloat(element.textContent.replace('$','')) * 1.1).toFixed(2); }); // 使用正则表达式替换页面中的所有数字 document.body.innerHTML = document.body.innerHTML.replace(/\d+/g, function(match) { return parseInt(match) + 100; }); // 修改表格中的数字 const tableCells = document.querySelectorAll('td'); tableCells.forEach(cell => { if (!isNaN(parseFloat(cell.textContent))) { cell.textContent = (parseFloat(cell.textContent) * 2).toString(); } });

2. 针对特定类型数字的修改

修改计数器/统计数字:


 
javascript
// 修改点赞数、观看数等 document.querySelector('.like-count').textContent = '1500'; document.querySelector('.view-count').textContent = '50000'; // 修改进度条数值 document.querySelector('.progress-percent').textContent = '75%'; document.querySelector('.progress-bar').style.width = '75%';

修改价格数字:


 
javascript
// 修改商品价格 document.querySelectorAll('.price').forEach(priceEl => { const originalPrice = parseFloat(priceEl.textContent.replace(/[^\d.-]/g, '')); priceEl.textContent = '$' + (originalPrice * 0.8).toFixed(2); }); // 修改折扣价 document.querySelector('.discount-price').innerHTML = '<strong>$29.99</strong>';

修改日期时间数字:


 
javascript
// 修改显示的时间 document.querySelector('.timestamp').textContent = '2023-12-25 14:30'; // 修改倒计时数字 document.querySelector('.countdown').textContent = '剩余: 2天 5小时 30分钟';

3. 使用用户脚本自动修改数字

Tampermonkey脚本示例:


 
javascript
// ==UserScript== // @name 数字修改器 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 自动修改网页上的特定数字 // @match https://example.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // 数字修改函数 function modifyNumbers() { // 修改特定类名的数字 document.querySelectorAll('.number-field').forEach(element => { const originalNumber = parseFloat(element.textContent); if (!isNaN(originalNumber)) { element.textContent = (originalNumber * 1.5).toFixed(0); } }); // 修改包含特定文本的数字 const allTexts = document.querySelectorAll('*'); allTexts.forEach(element => { if (element.childNodes.length === 1 && element.childNodes[0].nodeType === 3) { const text = element.textContent; const numberMatch = text.match(/(\d+)/); if (numberMatch) { const newNumber = parseInt(numberMatch[1]) + 100; element.textContent = text.replace(numberMatch[1], newNumber.toString()); } } }); } // 页面加载完成后执行 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', modifyNumbers); } else { modifyNumbers(); } // 监听动态内容加载 const observer = new MutationObserver(modifyNumbers); observer.observe(document.body, { childList: true, subtree: true }); })();

4. 创建浏览器扩展永久修改数字

manifest.json:


 
json
{ "manifest_version": 3, "name": "数字修改扩展", "version": "1.0", "content_scripts": [{ "matches": ["<all_urls>"], "js": ["content.js"] }] }

content.js:


 
javascript
// 数字修改逻辑 function modifyPageNumbers() { // 定义数字修改规则 const rules = [ { selector: '.score', multiplier: 2 }, { selector: '.rating', offset: 1 }, { selector: '.price', discount: 0.9 } ]; rules.forEach(rule => { document.querySelectorAll(rule.selector).forEach(element => { const originalValue = parseFloat(element.textContent); if (!isNaN(originalValue)) { let newValue = originalValue; if (rule.multiplier) newValue *= rule.multiplier; if (rule.offset) newValue += rule.offset; if (rule.discount) newValue *= rule.discount; element.textContent = newValue.toFixed(2); } }); }); } // 执行修改 modifyPageNumbers(); // 监听DOM变化 const observer = new MutationObserver(modifyPageNumbers); observer.observe(document.body, { childList: true, subtree: true });

5. 使用Bookmarklet快速修改数字

创建书签,URL填写以下JavaScript代码:

 
javascript
javascript:(function(){ // 简单的数字增加脚本 document.body.innerHTML = document.body.innerHTML.replace(/\b\d+\b/g, function(match) { return (parseInt(match) + 100).toString(); }); })();
或者更复杂的版本:

 
javascript
javascript:(function(){ var factor = prompt('请输入倍数:', '2'); if(factor) { document.querySelectorAll('*').forEach(el => { if(el.childNodes.length === 1 && el.childNodes[0].nodeType === 3) { el.textContent = el.textContent.replace(/\b\d+(\.\d+)?\b/g, function(num) { return (parseFloat(num) * parseFloat(factor)).toFixed(2); }); } }); } })();

6. 高级数字处理技巧

条件性数字修改:


 
javascript
// 只修改大于某个值的数字 document.querySelectorAll('.number').forEach(element => { const value = parseFloat(element.textContent); if (!isNaN(value) && value > 100) { element.textContent = (value * 1.2).toString(); } }); // 格式化大数字显示 function formatNumber(num) { if (num >= 1000000) { return (num/1000000).toFixed(1) + 'M'; } else if (num >= 1000) { return (num/1000).toFixed(1) + 'K'; } return num.toString(); } document.querySelectorAll('.large-number').forEach(element => { const value = parseInt(element.textContent.replace(/[^\d]/g, '')); element.textContent = formatNumber(value); });

修改图表中的数字:


 
javascript
// 修改Canvas图表中的数字(如果以文本形式显示) document.querySelectorAll('text.chart-label').forEach(label => { const value = parseFloat(label.textContent); if (!isNaN(value)) { label.textContent = (value * 1.5).toFixed(0); } });

重要说明

⚠️ 注意事项
  1. 临时性修改:使用开发者工具的修改仅在当前会话有效
  2. 仅限显示:这些方法只修改浏览器显示,不影响服务器数据
  3. 合法性:只能用于合法目的,不得用于欺诈或其他违法行为
  4. 网站功能:过度修改可能影响网站正常功能
  5. 刷新恢复:页面刷新后修改会失效(除非使用扩展程序)
您具体想修改哪类数字?我可以提供更有针对性的解决方案。
 

怎么修改网页源代码来修改数字(图文)

标签:

更新时间:2025-12-16 10:56:43

上一篇:怎么修改网页字体大小(图文)

下一篇:网页修改日期怎么改(图文)