在IE中克隆对象(.clone)的问题

时间:2010-12-04 20:52:00

标签: javascript jquery clone

我正在克隆一个flash,然后改变一些变量。这是功能:

$(function() {
  $("select[name='variation3']").live('change', function() {
    var player = $(this).parent().parent().parent().find('object:first');
    if(player.length == 0)
      player = $(this).parent().parent().parent().find('.player_not_single, .player_singele');

    var $param = player.find("param[name='flashvars']");
    var path = player.closest('.post1').attr('data-demo');

    //$param.attr('value', $param.attr('value').replace(/soundFile=([^&]+)/, 'soundFile=' + path + $(this).find(':selected').val() + '.mp3'));

    $param.attr("value", function(i,v) { return v.replace(/soundFile=([^&]+)/, 'soundFile=' + path + $(this).find(':selected').val() + '.mp3'); });

    var new_player = player.clone();
    new_player.insertBefore(player);
    player.remove();
  });
});

在FF中它工作正常,但是它会产生问题。这是克隆之前的代码:

<object name="audioplayer_1" width="270" height="24" id="audioplayer_1"
        classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
        style="outline-style:none; outline-color:invert; outline-width:medium;">  
  <param name="bgcolor" value="#FFFFFF"/>  
  <param name="wmode" value="transparent"/>  
  <param name="menu" value="false"/>  
  <param name="flashvars"  value="animation=no&encode=no&initialvolume=60&remaining=no&noinfo=yes&buffer=3&checkpolicy=no&rtl=no&bg=363635&text=333333&leftbg=4f4f4f&lefticon=787171&volslider=71bced&voltrack=777987&rightbg=4d4d4d&rightbghover=66a7d9&righticon=969696&righticonhover=000000&track=c2bebe&loader=5f91f5&border=000000&tracker=90b7d1&skip=b3b3b3&soundFile=http%3A%2F%2Fwww.gameaudio.net%2Fprelisten%2Fanotherworld%2Ffulledit.mp3&playerID=audioplayer_1"/>  
  <param name="movie" value="http://www.gameaudio.net/wp-content/plugins/audio-player/assets/player.swf?ver=2.0.4.1"/>  
</object>  

这是克隆后的代码:

<object name="audioplayer_1" width="270" height="24" id="audioplayer_1"
        classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
        style="outline-style:none; outline-color:invert; outline-width:medium;">  
  <param name="_cx" value="7143"/>  
  <param name="_cy" value="635"/>  
  <param name="FlashVars" value=""/>  
  <param name="Movie" value="http://www.gameaudio.net/wp-content/plugins/audio-player/assets/player.swf?ver=2.0.4.1"/>  
  <param name="WMode" value="Transparent"/>  
  <param name="BGColor" value="FFFFFF"/>  
  [...]  
</object>

不一样。部分是,但不是全部,尤其是缺少最重要的部分。 可能是什么错误?

更新: 这是我目前的代码: 'function embedAudioPlayer(replaceElemIdStr,audioFile){

swfobject.embedSWF(   
    "http://www.gameaudio.net/wp-content/plugins/audio-player/assets  /player.swf?ver=2.0.4.1",   
    replaceElemIdStr,   
    width,   
    height,   
    "9.0.115",   
    "expressInstall.swf",  
    {  
        soundFile: audioFile  
    });  

}

$(function(){

$(“select [name ='variation3']”)。live('change',function(){

player = $(this).parent().parent().parent().find('.player_not_single,   .player_singele');

var $param = player.find("param[name='flashvars']");  
var path = player.closest('.post1').attr('data-demo');  
var audioFile = path + $(this).find(':selected').val() + '.mp3';  
var uniqueId = 'audio-' + +new Date;  
player.replaceWith('<div id="+uniqueId+"/>');  
embedAudioPlayer(uniqueId, audioFile);  

});     “

function embedAudioPlayer(replaceElemIdStr,audioFile){ var swfobject = audioplayer_swfobject;
    swfobject.embedSWF(         “http://www.gameaudio.net/wp-content/plugins/audio-player/assets/player.swf?ver=2.0.4.1”         replaceElemIdStr,         250,         50,         “9.0.115”,         “expressInstall.swf”         {             soundFile:audioFile         }); }

$(function(){

$(“select [name ='variation3']”)。live('change',function(){

player = $(this).parent().parent().parent().find('.player_not_single, .player_singele');

var $param = player.find("param[name='flashvars']");
var path = player.closest('.post1').attr('data-demo');
var audioFile = path + $(this).find(':selected').val() + '.mp3';
var uniqueId = 'audio-' + +new Date;
player.replaceWith('<div id="+uniqueId+"/>');
embedAudioPlayer(uniqueId, audioFile);

});

更新

`   function embedAudioPlayer(replaceElemIdStr,audioFile){ var swfobject = audioplayer_swfobject;
    swfobject.embedSWF(         “http://www.gameaudio.net/wp-content/plugins/audio-player/assets/player.swf?ver=2.0.4.1”         replaceElemIdStr,         250,         50,         “9.0.115”,         “expressInstall.swf”         {             soundFile:audioFile         }); }

$(function(){

$(“select [name ='variation3']”)。live('change',function(){

player = $(this).parent().parent().parent().find('.player_not_single, .player_singele');

var $param = player.find("param[name='flashvars']");
var path = player.closest('.post1').attr('data-demo');
var audioFile = path + $(this).find(':selected').val() + '.mp3';
var uniqueId = 'audio-' + +new Date;
player.replaceWith('<div id="+uniqueId+"/>');
embedAudioPlayer(uniqueId, audioFile);

});`

1 个答案:

答案 0 :(得分:1)

Firefox和除IE之外的所有内容都使用Netscape Plugin Application Programming Interface (NPAPI)进行Flash。 Firefox(等)使用param元素,但IE从属性中读取。除了你使用类似that's what you have to do之类的东西(强烈推荐)之外,我很烦恼地指定所有内容但swfobject

基于使用swfobject更新:

听起来您实际上正在尝试使用swfobject.embedSWF重新实现jQuery.clone。我确信它可以工作,但我建议你坚持使用swfobject进行Flash嵌入和jQuery for DOM。我使用以下结构:

function embedAudioPlayer(replaceElemIdStr, audioFile) {
    // customize these options for the audio player you're using
    swfobject.embedSWF(
        "yourplayer.swf", 
        replaceElemIdStr, 
        width, 
        height, 
        "9.0.115", 
        "expressInstall.swf",
        {
            soundFile = audioFile
        });
}

$("select[name='variation3']").live('change', function() {
    // ...
    var audioFile = path + $(this).find(':selected').val() + '.mp3';
    var uniqueId = 'audio-' + +new Date;
    player.replaceWith('<div id="+uniqueId+"/>');
    embedAudioPlayer(uniqueId, audioFile);
});
  1. 创建一个embedAudioPlayer函数,并在初始页面加载和响应页面事件时将其用于所有内容。
  2. 响应页面事件时,使用jQuery确定soundFile网址和应创建新音频播放器的目标元素。