javascript jQuery 1.11.0 中的“无法读取属性...未定义”——我怎样才能满足 jQuery 的要求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22304935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Cannot read property ... of undefined" in jQuery 1.11.0 -- how can I satisfy jQuery's requirement?
提问by Christos Hayward
I was (trying to) load jQuery 1.9.1 un-minified from CDN: "Uncaught TypeError: Cannot call method 'call' of undefined jquery-1.9.1.js:648". So I updated the reference, and am apparently getting an equivalent error, now for minified:
我正在(试图)从 CDN 加载未缩小的 jQuery 1.9.1:“未捕获的类型错误:无法调用未定义的 jquery-1.9.1.js:648 的方法‘调用’”。所以我更新了参考,显然我得到了一个等效的错误,现在缩小:
Uncaught TypeError: Cannot call method 'call' of undefined jquery-1.11.0.min.js:2
n.extend.each jquery-1.11.0.min.js:2
(anonymous function) site.js:26
j jquery-1.11.0.min.js:2
k.fireWith jquery-1.11.0.min.js:2
n.extend.ready jquery-1.11.0.min.js:2
K jquery-1.11.0.min.js:2
The page is at http://ccachicago.pragmatometer.com; the first SCRIPT tag is:
该页面位于http://ccachicago.pragmatometer.com;第一个 SCRIPT 标签是:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
(The page is POSH plus CSS styling and a light touch of JavaScript dust; it's simple by modern webapp standards...)
(该页面是 POSH 加上 CSS 样式和轻微的 JavaScript 灰尘;按照现代 webapp 标准,这很简单......)
--EDIT--
- 编辑 -
Regarding my code, the page has, as its only script contents:
关于我的代码,该页面具有作为其唯一脚本内容:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="/media/js/site.js"></script>
<script>
function slideshow()
{
var width = (jQuery(window).width() - 70) * .8;
var factor = width / 1024;
var height = 420 * factor;
jQuery('#slideshow').attr('height', height + 'px');
jQuery('#slideshow').attr('width', width + 'px');
};
slideshow();
jQuery(window).resize(slideshow);
</script>
The site.js file has:
site.js 文件有:
/** * Override jQuery.fn.init to guard against XSS attacks. * * See http://bugs.jquery.com/ticket/9521 */ (function () { var jquery_init = jQuery.fn.init; jQuery.fn.init = function (selector, context, rootjQuery) { // If the string contains a "#" before a "= 0) { var bracket_position = selector.indexOf(' hash_position) { throw 'Syntax error, unrecognized expression: ' + selector; } } } return jquery_init.call(this, selector, context, rootjQuery); }; jQuery.fn.init.prototype = jquery_init.prototype; })(); jQuery(function() { jQuery.each(jQuery('h1').add('h2').add('h3').add('h4').add('h5').add('h6').add('ul#navigation')).function(index, element) { var bounds = jQuery(element).offset(); var image_left = bounds.left; var image_top = bounds.top + bounds.height; var image = jQuery(''); image.style.position = 'absolute'; image.style.top = image_top + 'px'; image.style.left = image_left + 'px'; } function set_search_width() { var offset = 950; var width = jQuery(window).width() - offset; jQuery('#menu-search').css('max-width', width + 'px'); jQuery('#menu-search').css('width', width + 'px'); jQuery('#query').width(width - 80); } jQuery(window).resize(set_search_width); set_search_width(); }); sfHover = function() { var sfEls = document.getElementById('nav').getElementsByTagName('li'); for(var index = 0; index
/** * Override jQuery.fn.init to guard against XSS attacks. * * See http://bugs.jquery.com/ticket/9521 */ (function () { var jquery_init = jQuery.fn.init; jQuery.fn.init = function (selector, context, rootjQuery) { // If the string contains a "#" before a "<", treat it as invalid HTML. if (selector && typeof selector === 'string') { var hash_position = selector.indexOf('#'); if (hash_position >= 0) { var bracket_position = selector.indexOf('<'); if (bracket_position > hash_position) { throw 'Syntax error, unrecognized expression: ' + selector; } } } return jquery_init.call(this, selector, context, rootjQuery); }; jQuery.fn.init.prototype = jquery_init.prototype; })(); jQuery(function() { jQuery.each(jQuery('h1').add('h2').add('h3').add('h4').add('h5').add('h6').add('ul#navigation')).function(index, element) { var bounds = jQuery(element).offset(); var image_left = bounds.left; var image_top = bounds.top + bounds.height; var image = jQuery('<img src="/media/images/foldback.png">'); image.style.position = 'absolute'; image.style.top = image_top + 'px'; image.style.left = image_left + 'px'; } function set_search_width() { var offset = 950; var width = jQuery(window).width() - offset; jQuery('#menu-search').css('max-width', width + 'px'); jQuery('#menu-search').css('width', width + 'px'); jQuery('#query').width(width - 80); } jQuery(window).resize(set_search_width); set_search_width(); }); sfHover = function() { var sfEls = document.getElementById('nav').getElementsByTagName('li'); for(var index = 0; index < sfEls.length; index++) { sfEls[i].onmouseover = function() { this.className += " sfhover"; } sfEls[i].onmouseout = function() { this.className = this.className.replace(new RegExp( " sfhover\b"), ""); } } } if (window.attachEvent) { window.attachEvent("onload", sfHover); }
回答by Vasiliy Vanchuk
You have incorrect syntxis
你的语法不正确
What do you whant to do with ?
你想做什么?
jQuery(function()
{
jQuery.each(jQuery('h1').add('h2').add('h3').add('h4').add('h5').add('h6').add('ul#navigation')).function(index, element)
{
var bounds = jQuery(element).offset();
var image_left = bounds.left;
var image_top = bounds.top + bounds.height;
var image = jQuery('<img src="/media/images/foldback.png">');
image.style.position = 'absolute';
image.style.top = image_top + 'px';
image.style.left = image_left + 'px';
}
function set_search_width()
{
var offset = 950;
var width = jQuery(window).width() - offset;
jQuery('#menu-search').css('max-width', width + 'px');
jQuery('#menu-search').css('width', width + 'px');
jQuery('#query').width(width - 80);
}
jQuery(window).resize(set_search_width);
set_search_width();
});
maybe code below is what you want ? ( find elements, i think it is better use complex selector, and apply function to each element )
也许下面的代码是你想要的?(查找元素,我认为最好使用复杂的选择器,并将函数应用于每个元素)
jQuery('h1').add('h2').add('h3').add('h4').add('h5').add('h6').add('ul#navigation').each(function(index, element)
{
var bounds = jQuery(element).offset();
var image_left = bounds.left;
var image_top = bounds.top + bounds.height;
var image = jQuery('<img src="/media/images/foldback.png">');
image.style.position = 'absolute';
image.style.top = image_top + 'px';
image.style.left = image_left + 'px';
});
回答by lalibi
The offending line is: site.js line 26
违规行是: site.js line 26
if should be something like
如果应该是这样的
jQuery.each(jQuery('h1').add('h2'), function(index, element) { ... })
not
不是
jQuery.each(jQuery('h1').add('h2')).function(index, element) { ... }