【问题标题】:WP Media Library/Uploader dont returns dynamic HTML/DOMWP 媒体库/上传不返回动态 HTML/DOM
【发布时间】:2013-01-30 05:03:26
【问题描述】:

样本来自Display Media Uploader in Own Plugin on Wordpress 3.5:

<script type="text/javascript">
var file_frame;

jQuery('.button-secondary').live('click', function( event ){

    event.preventDefault();

    if ( file_frame ) {
        file_frame.open();
        return;
    }

    file_frame = wp.media.frames.file_frame = wp.media(
        {
            title: 'Select File',
            button: {
                text: jQuery( this ).data( 'uploader_button_text' )
            },
            multiple: false
        }
    );

    file_frame.on('select', function() {
        attachment = file_frame.state().get('selection').first().toJSON();
        jQuery('#IMGsrc').val(attachment.url);
    });

    file_frame.open();
});

问题是关于

file_frame.on('select', function() {...});

不返回动态 html。我试过这样的代码:

jQuery(document).on('select', file_frame, function() {...});
jQuery(document).on('select', file_frame.el, function() {...});

但不工作...

【问题讨论】:

  • 为什么不用 .on( 而不是旧的 .live( ?
  • live 将被弃用,因为我知道...我喜欢 live((

标签: jquery wordpress media uploader wordpress-3.5


【解决方案1】:

window.send_to_editor(html) 出于兼容性原因即使在新的上传器中也会被调用,但我认为以这种方式调用它不会起作用。您可以使用您选择的对象的属性,并创建一个链接/图像 html。

【讨论】:

  • 是的,我可以,但我无法返回带有伪链接的图像,该伪链接将从此动态 html 元素中工作
  • 什么意思?你的代码attachment的值是多少?
  • attachment 是一个 JSON 对象,其中包含有关所选附件的信息
【解决方案2】:

我与我认为类似的问题作斗争。部分问题在于上传时,第一个附件对象为空并导致错误。

这是我的代码示例,可在我的网站上根据需要输出 HTML:

// 选择图像时,将 ID 放置在隐藏的自定义字段中并显示缩略图。 file_frame.on('选择', function() {

var selection = file_frame.state().get('selection');

// Show Thumbs
var attachment_thumbs = selection.map( function( attachment ) {
  attachment = attachment.toJSON();
  if( attachment.id != '' ) { return '<img src="' + attachment.sizes.thumbnail.url + '" id="id-' + attachment.id + '" />'; }
}).join(' ');
$('#images-feedback').show();
$('#thumbs').html(attachment_thumbs);

});

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 2011-09-22
    • 2016-10-14
    • 2013-03-20
    • 2020-01-14
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多