最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to target second child element of a div using JAVASCRIPT - Stack Overflow

programmeradmin0浏览0评论

I'm not sure if this is possible, I rarely do when I ask questions on this site, but everyone keeps telling me to use jQuery to do so. I make websites, but I am VERY novice at all of this and would prefer to get solid grip on Javascript before moving forward.

Anyways, my question is "Is it possible to target a second child (i.e.

<div>
   <div></div>
   <div></div> <!--This one-->
</div>

using JAVASCRIPT in order to manipulate it? If so, how?"

My previous attempt didn't work, I had a strong feeling it wouldn't but, all the same.

function activateEvent(obj) {
    obj.secondChild.style.display="block";};

I'm not sure if this is possible, I rarely do when I ask questions on this site, but everyone keeps telling me to use jQuery to do so. I make websites, but I am VERY novice at all of this and would prefer to get solid grip on Javascript before moving forward.

Anyways, my question is "Is it possible to target a second child (i.e.

<div>
   <div></div>
   <div></div> <!--This one-->
</div>

using JAVASCRIPT in order to manipulate it? If so, how?"

My previous attempt didn't work, I had a strong feeling it wouldn't but, all the same.

function activateEvent(obj) {
    obj.secondChild.style.display="block";};
Share Improve this question edited Aug 14, 2013 at 13:50 Hashem Qolami 99.6k27 gold badges155 silver badges165 bronze badges asked Aug 14, 2013 at 13:37 Logan CersonLogan Cerson 771 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9
obj.firstElementChild.nextElementSibling

(doesn't work in IE8)

Or

obj.children[1]

You really should try to learn jQuery because it will make things a lot easier for you. Get the basics from plain Javascript and then learn a library like jQuery. you just need to load it into your website, then you can target the element in your exemple using: $('.parent div:nth-child(2)') It's that easy.

发布评论

评论列表(0)

  1. 暂无评论