在 javascript 中设置属性时未捕获类型错误
Uncaught TypeError on setting attribute in javascript
经过大量研究,我终于找到了如何在 Chrome DevTools 中使用 javascript 设置网页的初始比例。代码应该是这样的:
document.getElementById("viewport").setAttribute("content", "initial-scale=0.5, user-scalable=yes");
但是,我总是收到错误消息:Uncaught TypeError: Cannot read property 'setAttribute' of null(…)
我在不同的页面上试过了。有什么改变了吗?我在不同的帖子中找到了这个方法。
错误提示页面上没有id为"viewport"
的元素。您可能希望修改 viewport meta tag instead, in which case this answer 上的属性描述使用 JavaScript 的正确方法。
我先尝试使用 document.getElementById("viewport");
,后来又使用您的完整代码,结果成功了。
截图结果如下:
所以,也许试试这个。调用 document.getElementById("viewport");
如果 if 不为 null 或 undefined,则执行:
document.getElementById("viewport").setAttribute("content", "initial-scale=0.5, user-scalable=yes");
经过大量研究,我终于找到了如何在 Chrome DevTools 中使用 javascript 设置网页的初始比例。代码应该是这样的:
document.getElementById("viewport").setAttribute("content", "initial-scale=0.5, user-scalable=yes");
但是,我总是收到错误消息:Uncaught TypeError: Cannot read property 'setAttribute' of null(…)
我在不同的页面上试过了。有什么改变了吗?我在不同的帖子中找到了这个方法。
错误提示页面上没有id为"viewport"
的元素。您可能希望修改 viewport meta tag instead, in which case this answer 上的属性描述使用 JavaScript 的正确方法。
我先尝试使用 document.getElementById("viewport");
,后来又使用您的完整代码,结果成功了。
截图结果如下:
所以,也许试试这个。调用 document.getElementById("viewport");
如果 if 不为 null 或 undefined,则执行:
document.getElementById("viewport").setAttribute("content", "initial-scale=0.5, user-scalable=yes");