如何获取< noscript>的内容在IE7中的Javascript? [英] How to get content of <noscript> in Javascript in IE7?

查看:99
本文介绍了如何获取< noscript>的内容在IE7中的Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript获取< noscript> 标记的内容。我成功地在FF,Chrome,Opera甚至IE6中获得了它,但在IE7上失败了(还没有尝试过IE8 +)。

I'm trying to get the content of a <noscript> tag using Javascript. I succesfully managed to get it in FF, Chrome, Opera and even IE6 but fail on IE7 (haven't tried IE8+ yet).

基本上,这是减少的代码版本:

Basically, here's the reduced code version :

<noscript>Lorem ipsum</noscript>
<script>
    var noscript = document.getElementsByTagName('noscript')[0];
    noscript.textContent; // undefined
    noscript.innerHTML; // empty string
    noscript.childNodes.length; // 0
</script>

我尝试在里面添加元素并定位它们,但没有成功。我试图包装一个父元素并获取其 .innerHTML ,但是< noscript> 标签之间的任何内容都被丢弃。

I tried adding element inside and targeting them, no success. I tried to wrap in a parent element and getting its .innerHTML, but anything between <noscript> tags is discarded.

注意:我正在构建一个lazyloader脚本,< noscript> 元素正是我需要的(< img> src < noscript> 标签不是由浏览器提取的。)

Note : I'm building a lazyloader script and the <noscript> element is just what I need (<img> src attributes inside a <noscript> tag are not fetched by the browser.)

推荐答案

在IE 7和8中,根本不可能检索到的内容一个< noscript> 元素。 HTML中的< noscript> < / noscript> 标记之间的任何内容都不会反映在DOM中在IE中,元素没有子元素, innerHTML innerText 是空字符串。

In IE 7 and 8, it's simply impossible to retrieve the contents of a <noscript> element. Any content between the <noscript> and </noscript> tags in the HTML is not reflected in the DOM in IE, the element has no children and innerHTML and innerText are empty strings.

在IE 6中,情况很奇怪:与IE 7一样,< noscript> 元素没有子节点,但其内容反映出来在 innerHTML outerHTML (但不是 innerText )属性元素。

In IE 6, the situation is curious: in common with IE 7, the <noscript> element has no child nodes but its contents are reflected in the innerHTML and outerHTML (but not innerText) properties of the element.

所有这一切都是如此,IE中你唯一的选择就是把内容放在< noscript> 替换其他元素中的元素。要模拟< noscript> 元素的行为,您可以将内容放在一个立即被JavaScript隐藏的元素中(启用脚本时):

All this being the case, your only option in IE is to put the content in your <noscript> element inside some other element instead. To emulate the behaviour of a <noscript> element, you could put the content in an element that is immediately hidden by JavaScript (when script is enabled):

<div id="noscript">Lorem ipsum</div>
<script type="text/javascript">
    document.getElementById("noscript").style.display = "none";
</script>

这篇关于如何获取&lt; noscript&gt;的内容在IE7中的Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆