无法使用jQuery(IE 8)访问文档的标题元素 [英] Cannot access document's title element with jQuery (IE 8)

查看:93
本文介绍了无法使用jQuery(IE 8)访问文档的标题元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我正在开发自己的jQuery插件,对于这个问题,我已经把它删除了两个相关






在IE 8中,使用以下代码 $('title')。 text()不做任何事情。 docTitle 是空白的,因为 title 是空白的,就好像jQuery选择器< title> $('title')不工作。 (再次,AFAIK,这只是在IE 8)

 (function($){
$ .fn。 myPlugin = function(options){

var title = $('title')。text(),
docTitle = escape(title);

};
})(jQuery);

http://jsfiddle.net/sparky672/YMBQ2/






然而,使用平原下面的JavaScript代码 document.title 在包括IE 8 ...中的所有内容都可以正常工作。

 (function($){
$ .fn.myPlugin = function(options){

var docTitle = escape(document.title);

};
})(jQuery);






编辑: / p>

这个代码在插件里面并不重要。



与IE8相同的结果

  $(document).ready(function(){
var title = $('title')。 text();
alert(title);
});

只是为了澄清,我不坚持使用这个。事实上,我通过简单地使用 document.title 来修复我的插件。 如果最初不清楚,我只是问为什么这在IE 8中不起作用。






任何人都可以解释为什么,或者我可能在这里犯了什么愚蠢的错误?






编辑2:



以下是关于此问题的一些jQuery Bug报告



http://bugs.jquery.com/ticket/7025



http://bugs.jquery.com/ticket/5881



http://bugs.jquery.com / ticket / 2755



还有几十个人报告同样的事情。官方的回复是声明, document.title 是唯一可靠的跨浏览器方式,应该使用并且机票已关闭。所以你去。

解决方案

我猜jQuery遍历所有TextNodes并连接其nodeValue。 IE存储此值与其他浏览器不同。

  var title = document.getElementsByTagName('title')[0]; 
title.firstChild //这将是具有标题的字符数据的文本对象
// Firefox:[object Text]
// IE:null

这应该是您无法使用获取textContent的原因jQuery.text() title.text 似乎是跨浏览器。我只在IE 7和Firefox 3.6中进行了测试,但如果您愿意,您可以查看其他浏览器。但是为什么不使用 document.title


I'm seeing this issue in Internet Explorer 8, but not in Safari or Firefox. So far, I have not tested in other IE versions.

I am developing my own jQuery plugin and, for this question, I've stripped it down to the two relevant lines.


In IE 8, using the code below, $('title').text() does not do anything. docTitle is blank because title is blank, as if the jQuery selector for <title>, $('title') is not working. (Again, AFAIK, this is just in IE 8)

(function ($) {
    $.fn.myPlugin = function (options) {

        var title = $('title').text(),
            docTitle = escape(title);

    };
})(jQuery);

http://jsfiddle.net/sparky672/YMBQ2/


However, using the plain JavaScript code below, document.title is working fine in everything including IE 8...

(function ($) {
    $.fn.myPlugin = function (options) {

        var docTitle = escape(document.title);

    };
})(jQuery);


EDIT:

It does not matter that this code is inside a plugin.

Same result in IE 8 with this...

$(document).ready(function () {    
    var title = $('title').text();
    alert(title);
});

Just to clarify, I am not insisting on using this. In fact, I fixed my plugin by simply using document.title instead. If it wasn't clear initially, I'm just asking why this does not work in IE 8.


Can anyone explain why, or what stupid mistake I may have made here?


EDIT 2:

Here are some jQuery Bug reports on this issue

http://bugs.jquery.com/ticket/7025

http://bugs.jquery.com/ticket/5881

http://bugs.jquery.com/ticket/2755

And dozens of others reporting the same thing. The official response is to state, "document.title is the only reliable cross-browser way and should be used instead" and the Ticket is closed. So there you go.

解决方案

I guess jQuery iterates over all TextNodes and concatenates its nodeValue. IE stores this value differently than other browsers.

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

This should be the reason you cannot get the textContent with jQuery.text(). title.text seems to be cross browser comp. I only tested it in IE 7 and Firefox 3.6 but you can check the other browser if you like. But why not using document.title?

这篇关于无法使用jQuery(IE 8)访问文档的标题元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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