admin管理员组文章数量:1289548
Considering that I have an element like this:
<p class="ELP">2/1/2013 - <a id="EL3" class="ELLink" href="/Event htms/Event.cshtml?title=Okmulgee Public Schools County Professional Day&explanation=Okmulgee Public Schools County Professional Day.&dateString=2-1-2013">City Public Schools County Professional Day</a></p>
Why does this JavaScript/jQuery work...:
$(".ELP").click(function (){
var stringDate = $(this).text().substring(0, 8)
console.log(stringDate)
});
Console.log produces: 2/1/2013
...And this JavaScript/jQuery doesn't?
$(".ELLink").click(function (){
var stringDate = $(this).parentNode.text().substring(0, 8)
console.log(stringDate)
});
Console.log produces nothing because of JavaScript error: Uncaught TypeError: Cannot call method 'text' of undefined
Clearly I can see that it is not 'getting' the right element, but why? Isn't the first parent node of my 'a' element the 'p' element? As far as I understand it, there is no (at least no cross-browser/platform patible) valid css selector for the direct parent node of an element or i would just use that.
What am I not seeing?
Considering that I have an element like this:
<p class="ELP">2/1/2013 - <a id="EL3" class="ELLink" href="/Event htms/Event.cshtml?title=Okmulgee Public Schools County Professional Day&explanation=Okmulgee Public Schools County Professional Day.&dateString=2-1-2013">City Public Schools County Professional Day</a></p>
Why does this JavaScript/jQuery work...:
$(".ELP").click(function (){
var stringDate = $(this).text().substring(0, 8)
console.log(stringDate)
});
Console.log produces: 2/1/2013
...And this JavaScript/jQuery doesn't?
$(".ELLink").click(function (){
var stringDate = $(this).parentNode.text().substring(0, 8)
console.log(stringDate)
});
Console.log produces nothing because of JavaScript error: Uncaught TypeError: Cannot call method 'text' of undefined
Clearly I can see that it is not 'getting' the right element, but why? Isn't the first parent node of my 'a' element the 'p' element? As far as I understand it, there is no (at least no cross-browser/platform patible) valid css selector for the direct parent node of an element or i would just use that.
What am I not seeing?
Share Improve this question asked Feb 1, 2013 at 16:37 VoidKingVoidKing 6,4329 gold badges51 silver badges83 bronze badges 3-
3
jQuery objects don't have a
parentNode
property. try using the.parent()
method instead. – Kevin B Commented Feb 1, 2013 at 16:38 -
1
use
parent()
instead ofparentNode
– N.B. Commented Feb 1, 2013 at 16:39 - Thanks for the help, guys, every search I did on this referred JavaScript parentNode property. Didn't really see the way jQuery implements this, but I get it now, thanks to all of you. But you would think I would have gotten some information on the parent() method. Again, thanks! – VoidKing Commented Feb 1, 2013 at 16:51
5 Answers
Reset to default 7The jQuery method is called parent()
$(".ELLink").click(function (){
var stringDate = $(this).parent().text().substring(0, 8)
console.log(stringDate)
});
Because the jQuery object does not have a parentNode property.
var stringDate = $(this.parentNode).text().substring(0, 8);
$(...)
does not return DOM elements, it returns an array-like object that references DOM nodes. DOM nodes have parentNode
. jQuery instances do not.
If you need to select the immediate parents of the selected elements, you can use parent()
:
$(this).parent().text()...
Why not use:
var stringDate = $(this).parent().text().substring(0, 8)
Since you're already using jQuery, you can use the parent function.
You need to use parent()
not parentNode
. Like this:
$(".ELLink").click(function (){
var stringDate = $(this).parent().text().substring(0, 8)
console.log(stringDate)
});
本文标签: Why can39t javascriptjQuery get parentNodeStack Overflow
版权声明:本文标题:Why can't javascriptjQuery get parentNode? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741419346a2377710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论