What is this.parentElement?
pg.myfunc = function(){
var i = 1, j = 2;
this.selected = 1;
xx.newObject = this.parentElement开发者_JAVA技巧;
...
What is xx.newObject = this.parentElement;
doing?
It's the same as this.parentNode
: it gives you the node that contains this
as a childNode. this
will be pg
, presumably an Element of some kind; this.parentNode
will be the Element that contains it, or the document
object if pg
is the root element.
parentElement
is a non-standard IE extension. Since IE also supports the standard property parentNode
, parentElement
should never be used.
Alternatively, maybe it's just an arbitrary object with a property called parentElement
, in which case it could be anything at all. There's no real way to tell from that code, but it would be unusual to be setting arbitrary properties like myfunc
on an Element node.
It saves a reference to the parent element of this
. So for example:
<div id="parent">
<span id="child">
</span>
</div>
In this case, if this
corresponds to the child
span, parentElement
would correspond to the parent
div.
That said, you should always use parentNode
instead of parentElement
, as parentElement
is proprietary and (I believe) only works with IE. According to MSDN:
There is no public standard that applies to this property.
It's impossible to know without some context. We don't what xx is, nor do we know for sure what the "this" object is. parentElement MAY be a property to pg, but then again this function may be called in a different way, for instance if somewhere it is assigned as an onclick function:
someElement.onclick = pg.myfunc;
In which case it would be a property of someElement.
As others have said, if "this" is a DOM element, you should be using parentNode not parentElement, since the latter is non-standard.
精彩评论