我正在尝试在jQuery中检索特定HTML元素的CSS选择器。出于这个原因,我有以下代码:
$(window).hover(function(e)
{
var x = e.clientX, y = e.clientY,
focusElement = document.elementFromPoint(x, y);
// GetPath.js
getPath: function( path )
{
// The first time this function is called, path won't be defined.
if ( typeof path == 'undefined' ) path = '';
// If this element is <html> we've reached the end of the path.
if ( this.is('html') )
return 'html' + path;
// Add the element name.
var cur = this.get(0).nodeName.toLowerCase();
// Determine the IDs and path.
var ID = this.attr('id'),
CLASS = this.attr('class');
// Add the #id if there is one.
if ( typeof ID != 'undefined' )
cur += '#' + ID;
// Add any classes.
if ( typeof CLASS != 'undefined' )
cur += '.' + CLASS.split(/[\s\n]+/).join('.');
// Recurse up the DOM.
return this.parent().getPath( ' > ' + cur + path );
}
$(focusElement).click(function(e)
{
e.preventDefault();
var path = $(focusElement).getPath();
$("#some", window.parent.document).text(path);
});
});
然而,无论如何我总是得到一个错误&#34; Uncaught TypeError:undefined不是一个函数&#34;用于getPath函数。无论我将函数放入click函数,悬停函数,悬停函数之外还是document.ready函数中,或者只是将它放在外部JS文件中,都会发生此错误。我的错误在哪里?