2012-07-17 209 views
1

我正在嘗試在自定義插件中使用wordpress的媒體上傳彈出窗口。jQuery:無法選擇鏈接

到目前爲止一切正常,我能夠上傳圖像並將它們插入到一個字段中,但我有另一個字段需要鏈接到上傳的PDF。

tb_show('', 'media-upload.php?type=image&TB_iframe=true;height=200'); 
window.send_to_editor = function(html) { 
    console.log(html); 

    console.log(jQuery('a', html)); 
    console.log(jQuery('a', html).attr('href')); 

    console.log(jQuery('img', html)); 
    console.log(jQuery('img', html).attr('src')); 

    imgurl = jQuery('a', html).attr('href'); 
    current_upload.prev().val(imgurl); 

    tb_remove(); 
} 

對於圖像,這將是我的輸出,這是正確的,因爲它能夠選擇圖像源

<a href="http://to.local/wp-content/uploads/2012/07/Screen-Shot-2012-06-20-at-12.15.52-PM.png"><img src="http://to.local/wp-content/uploads/2012/07/Screen-Shot-2012-06-20-at-12.15.52-PM-300x221.png" alt="" title="Screen Shot 2012-06-20 at 12.15.52 PM" width="300" height="221" class="alignnone size-medium wp-image-49" /></a> to.js:54 
[] 
undefined 
[ 
    <img src=​"http:​/​/​to.local/​wp-content/​uploads/​2012/​07/​Screen-Shot-2012-06-20-at-12.15.52-PM-300x221.png" alt title=​"Screen Shot 2012-06-20 at 12.15.52 PM" width=​"300" height=​"221" class=​"alignnone size-medium wp-image-49">​ 
] 
http://to.local/wp-content/uploads/2012/07/Screen-Shot-2012-06-20-at-12.15.52-PM-300x221.png 

但是,當我選擇一個PDF我得到這個:

<a href='http://to.local/wp-content/uploads/2012/07/01-Mobile-Characteristics-and-Interaction-Design-Principles-Slides2.pdf'>01 Mobile Characteristics and Interaction Design Principles (Slides)</a> to.js:54 
[] 
undefined 
[] 
undefined 

所以我想不出爲什麼jQuery('img',html)工作正常,而jQuery('a',html)沒有,而在這兩種情況下,在返回的HTML中有一個鏈接。

回答

2

假設html = jQuery('<a href="http://to.local/wp-content/uploads/2012/07/Screen-Shot-2012-06-20-at-12.15.52-PM.png"><img src="http://to.local/wp-content/uploads/2012/07/Screen-Shot-2012-06-20-at-12.15.52-PM-300x221.png" alt="" title="Screen Shot 2012-06-20 at 12.15.52 PM" width="300" height="221" class="alignnone size-medium wp-image-49" /></a>');html<a>元件。

jQuery('a', html)試圖選擇的html,這不返回任何結果孩子們在<a>因爲沒有<a>元素存在。

由於<img><a>父親的子女html,jQuery('img', html)的作品。

你的情況,得到的htmlhref屬性,這樣做:

jQuery(html).attr("href") 

(刪除jQuery()如果html已經是一個jQuery對象)

+0

哦,那更有意義,我是有點困惑,但知道是實際的html清除它。 – 2012-07-17 04:25:56