Android 不尊重元标记删除?

发布于 2024-09-16 18:31:15 字数 876 浏览 6 评论 0原文

我创建了一个示例脚本来添加和删除头部的元标记。但Android 2.2似乎并不尊重它的删除。然而,它确实尊重点击时添加元标记。例如,我如何让它尊重标记的删除并通过 JavaScript 恢复到默认视口?

<script type="text/javascript">

$(document).ready(function(){
function initMeta(){
var headID = document.getElementsByTagName("head")[0];         
var metaNode = document.createElement('meta');
metaNode.name = 'viewport';
metaNode.content = 'width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0';
metaNode.id = 'metatag';
headID.appendChild(metaNode);}

function closeMeta(){
$("#metatag").remove();}


$("#add").click(function(){initMeta();alert("meta opened");});
$("#del").click(function(){closeMeta();alert("meta closed");});

});

</script>

<input name="add" type="button" value="add metatag" id="add"/>
<input name="del" type="button" value="delete metatag" id="del"/>

I created a sample script to add and remove metatags from the head. But Android 2.2 doesn't seem to respect it's removal. However it does respect the addition of the metatag on click for example.. How do I get it to respect the removal of the tag and revert to the default viewport through javascript?

<script type="text/javascript">

$(document).ready(function(){
function initMeta(){
var headID = document.getElementsByTagName("head")[0];         
var metaNode = document.createElement('meta');
metaNode.name = 'viewport';
metaNode.content = 'width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0';
metaNode.id = 'metatag';
headID.appendChild(metaNode);}

function closeMeta(){
$("#metatag").remove();}


$("#add").click(function(){initMeta();alert("meta opened");});
$("#del").click(function(){closeMeta();alert("meta closed");});

});

</script>

<input name="add" type="button" value="add metatag" id="add"/>
<input name="del" type="button" value="delete metatag" id="del"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-09-23 18:31:15

我在 iOS Safari 中也注意到了这种行为。

您实际上正在删除元标记(可通过 DOM 验证 - 删除后尝试警告 $("#metatag").length )

问题是视口本身不会响应此标记中内容的缺失。如果您更新内容或使用新内容重新添加元标记,您应该会在屏幕上看到它。但通过简单地删除元标记,UA 似乎认为“无需更改”,因为它没有在那里得到任何明确的指示。

希望有帮助!你的问题/例子帮助我意识到这是可能的!

I note this behavior in iOS Safari too.

You are actually removing the meta tag (verifiable through DOM - Try alerting $("#metatag").length after removal)

The problem is the viewport itself does not respond to the absence of content in this tag. If you update the contents or re-add the meta tag with new contents, you should see it manifest on your screen. But by simply removing the meta tag, the UA seems to think "No change necessary" as it's not getting any explicit instructions there.

Hope that helps! Your question / example helped me realize this was even possible!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文