开发者

Finding first node of a page?

Here is my jQuery code:

$('body').append('<div id="popupContact"><a id="popupContactClose">close</a><br/><iframe src="http://google.com"></iframe></div><div id="backgroundPopup"></div>');

I tested it at this site.

It 开发者_开发百科worked but I have a problem. I think that the site has many iframes, so I want to find the first body of the combined page. I tried this code.

$('body')[0].append('<div id="popupContact"><a id="popupContactClose">close</a><br/><iframe src="http://google.com/" id = myFrame></iframe></div><div id="backgroundPopup"></div>');

But unfortunately it doesn't work.

I want to apply the rule only to the first body tag.

How can I solve this?


Try this:

$('body').first();

Or this:

$('iframe').first();


You could try $('body:first'). See http://api.jquery.com/first-selector for details.


I was trying to do this too, but found that both $('body:first') and $('body').first() still grabbed the first node of each iframe on the page.

Essentially what I was trying to do was add a div. Using top.document.body was what solved the issue for me. As in:

top.document.body.appendChild(mydiv); 

As pointed out on this page, I was looking for the 'true' body of the page: How to refer to the true 'body' of a page? [NOT iFrame body]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜