开发者

jQuery - Insert HTML into div, then get first child of inserted HTML

I've been working on a problem with a project for a while. Basically, I use .html() to load entries into a div. It all looks fine, but I tried to get the value of the :first-child, and it wouldn't work unless I put the entries directly into the HTML (as in, the source file) -- but I can't do that because the entries need to load dynamically (via JS).

So, I went to look at the page source to see what's up, and nothing showed up in the div where my entries were being loaded. I checked Firebug, and it showed that they were inside that div. The webpage itself showed that they were inside that div. But the page source did not.

Why wouldn't the HTML loaded by jQuery appear in the page source?

Updated


Alright, you guys answered why - so, I guess my real question is "how" to do what I'm trying to do -

The div loads several entries into #inner-wrap that look somewhat like:

<div class="entries">
    <input type="submit" value="1295843239" style="display: none;" class="realtime">
    <span class="time">1295843239:</span>
    <span class="name">Name: </span>
    <span class="message">this is an entry...</span>
</div>

How would I select the value of the input of the first child of #inner开发者_开发知识库-wrap?

I was using $("#inner-wrap :first-child .realtime").val(), but that didn't work when the entries were loaded dynamically. Here's all the related code, and here's the "live" bits of what I have so far. It's nowhere near done, but I need to be able to get the time of the last comment to check for new comments.


Dynamic html will not show in the source when using jQuery unless a postback has occurred. You would need to use a tool such as Firebug to verify the contents or force a postback by adding a form and posting to the same page.

<form id="myform" method="post" action="test.html">
<div id="mycontent">
</div>
</form>

<script>
$('#mycontent').html('Hello World!');
$('#myform').submit();
</script>


Stuff added to the DOM after the page is loaded doesn't show up in the "View Source" view because, simply, it is not in the source of the page. Use Firebug or the Chrome inspector tool to look at the DOM.

Post the details of your actual problem if you want help with it. It's far more likely that there's something amiss with your code than that there's a jQuery bug, but if you post your stuff it'll be possible for people to figure it out.


I've created a script that will encode an entire html file based on the actual data. Take a look here to see how it's done.

Alternatively you can open firebug and save the source that firebug has by right clicking on <html> then save html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜