2013-01-22 16 views
0

好了,所以這是快速啓動,以我的jQuery使用.load()的一個變量,複式標識符

var uid = $('#mem_week a').attr('href'); 
$('#usersImg').load(uid #profile-advanced-right img); 

不太清楚,如果我可以寫了var uid的.load()這樣的裏面,加我需要具體的ID #profile-advanced-right img這是可能的?如果不是有人可以證明我會怎麼寫?>

生成標記::

<div id="mem_week"> 
<h3> 
    <a href="/u1">Mr.EasyBB</a> 
</h3> 
    <div id="usersImg"></div> 
</div> 

這僅僅是一個基本的標記。真的,我想從股利#profile-advanced-right .main-content img彷彿抓住從URL/U1用戶圖像和出落得應該

<div id="mem_week"> 
<h3> 
    <a href="/u1">Mr.EasyBB</a> 
</h3> 
    <div id="usersImg"> 
    <img src="usersimg.png"/> 
    </div> 
</div> 
+1

'.load(UID +「#型材先進右IMG」)' – Jon

回答

3

關閉。這是你想要什麼:

$('#usersImg').load(uid + " #profile-advanced-right img"); 

你可以通過閱讀jQuery的load()文檔已經發現了這一點。在SO上提出問題之前檢查文檔通常是一個好主意。 :)

+0

我不認爲這就是他說的 - 我的讀書是url有一個html片段,他想從該html片段中獲取img –

+0

除非它是URL的哈希部分,否則應該添加一個空格? – adeneo

+0

@adeneo - 這沒有任何意義,url的哈希部分不會發送到服務器 –

2

所以負載基本上做同樣的事情,因爲這:

$.get(url).done(function(htmlFragment){ //get the html fragment at this url (it will be a string) 
    $('#myElement').html(htmlFragment); //set the contents of your element to that html fragment 
}); 

換句話說,它是一個更復雜的概念一個輔助方法。通過我的閱讀,你想要的是返回的片段中選擇一個img,貼在你的元素

var url = $('#mem_week a').attr('href'); 
$.get(url).done(function(htmlFragment){ //get the html fragment at this url (it will be a string) 
    //parse the string with jquery and navigate to that node 
    var justTheImg = $(htmlFragment).find('#profile-advanced-right img'); 
    //set the contents of your element to that img (using the html method might also work) 
    $('#myElement').append(justTheImg); 
});