【问题标题】:Override meta tag with a new meta tag用新的元标记覆盖元标记
【发布时间】:2018-12-10 23:09:52
【问题描述】:

我正在处理一个 ASP 页面,我想用 "meta http-equiv="X-UA-Compatible" 覆盖 "meta http-equiv="X-UA-Compatible" content="IE=8"" “ content="IE=10"” 在其中一个使用 JavaScript 的页面中。是否可以?我尝试了以下但没有成功。

    window.onload = function(e){ 
    $('meta[http-equiv="X-UA-Compatible"]').remove();
    $('head').append('<meta http-equiv="X-UA-Compatible" content="IE=10">')
}

【问题讨论】:

  • 或者你可以使用attr函数,比如.attr("content","IE=10"),而不是删除
  • @Manjunath:我尝试过像 $('meta[http-equiv="X-UA-Compatible"]').attr('content','IE=10'); 这样的 attr 函数它仍然没有工作
  • 你能告诉我们你在哪里以及如何包含这个脚本吗?
  • 这行不通,因为当window.onload 被触发时,元标记已经被读取,兼容性标志被设置,并且文档在那个模式下被解析。这需要在服务器上进行更改。

标签: javascript jquery


【解决方案1】:

有可能,但您需要致电.remove()(错过了()

虽然我不确定你希望完成的工作是否真的有效(如果你想让 IE 表现不同的话)

【讨论】:

  • 我意识到这一点并添加了缺失的“()”。但是,它仍然不起作用。
【解决方案2】:

把你的答案放在 $(document).ready() 中。由于您使用的是 jQuery,您可以这样做:

$(function() {
    $('meta[http-equiv="X-UA-Compatible"]').remove();
    $('head').append('<meta http-equiv="X-UA-Compatible" content="IE=10">');
});

这里很好地解释了window.onload$(document).ready() 之间的区别: window.onload vs $(document).ready()

【讨论】:

    【解决方案3】:

    试试这个:

    jQuery

    if (window.location.pathname === 'myPage') { // URL is example.com/myPage
        $('meta[http-equiv="X-UA-Compatible"]').replaceWith('<meta http-equiv="X-UA-Compatible" content="IE=10">'); 
    }
    

    原版 JS

    if (window.location.pathname === 'myPage') { // URL is example.com/myPage
        document.querySelector('meta[http-equiv="X-UA-Compatible"]').outerHTML = '<meta http-equiv="X-UA-Compatible" content="IE=10">';
    }
    

    【讨论】:

    • @user9808783 究竟是什么不起作用?您收到错误消息吗?如果没有 if 语句,它可以工作吗?
    • @Reza:不工作,我的意思是包含 content="IE=8" 的元标记没有被替换为 "IE=10"
    • @user9808783 我已经在 J​​SFiddle 上对其进行了测试,它对我有用。 jsfiddle.net/RezaScript/2ozpc9t8
    • @Reza:我看到你使用的是 Jquery 3.3.1。不幸的是,我使用的是 1.6.1。那个版本有没有办法
    • 我也尝试过 attr(),如果我正确阅读了 1.6.1 的文档,但它也没有工作
    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多