2012-07-17 154 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