jsGears.com 技術論壇 - AJAX, JavaScript, jQuery, 網站開發, 前端效能優化's Archiver

inkiness 發表於 2010-8-15 19:02

jQuery新手問題

[i=s] 本帖最後由 inkiness 於 2010-8-15 20:48 編輯 [/i]

我這裡是在second.html用ajax從first.html拿資料

first.html[code]<div id="first">第一行</div>
<div id="third">第三行</div>[/code]second.html[code]<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js" type="text/javascript"></script>
<input type="button" id="change" value="Change" />
<div id="second">第二行</div>

<script type="text/javascript">
        $("#change").click(function() {
                $.ajax({
                        type: "GET",
                        url: "first.html",
                        dataType: "html",
                        success: function(data){
                                alert(data);
                                $("#second").html(data);
                        }
                });
        });
</script>[/code]data返回時是<div id="first">第一行</div>
<div id="third">第三行</div>

我要怎樣才可從data中只拿到id=third (第三行) 這個

wmh 發表於 2010-8-16 07:37

可以先把字串塞入 jQuery 變成物件,再以 selector 操作。
[code js]
var $html = $('<div>' + html + '</div>');
var new_str = $html.find("#thrid")[0];
$("#second").html(new_str);
[/code]

inkiness 發表於 2010-8-16 11:01

謝謝大大幫忙:loveliness:

頁: [1]

Powered by Discuz! Archiver  © 2001-2009 Comsenz Inc.