代码之家  ›  专栏  ›  技术社区  ›  vondip

使用Ajax更改HTML中对象的参数值

  •  0
  • vondip  · 技术社区  · 16 年前

    我正在构建一个运行MIDI文件的小型Web应用程序。目前我正在使用快速时间播放MIDI文件。问题是,在用户输入一些信息(比如搜索)之前,我不知道需要将对象绑定到什么源。我需要能够将QuickTime电影更新到正确的路径。有可能吗?Ajax支持这个吗?

    2 回复  |  直到 16 年前
        1
  •  2
  •   Cristian Toma    16 年前

    Ajax是一种技术。您要做的是用JavaScript更改QuickTime电影路径。

    var qtMovie=document.getElementById('yourMovieEmbedID');
    qtMovie.src='your new source';
    

    您应该将此代码包装在一个函数中,并在用户单击“确定”按钮时运行它。

        2
  •  0
  •   Community CDub    8 年前

    如果你真的需要 添加 <param> (与调整属性值相反),我发现 <object> ,克隆它,添加 <PARAM-GT; 然后更换电流 <对象& 标签在大多数浏览器中似乎都有效(尽管它在Firefox中似乎不起作用)。见 this answer 例如。

    这个联系的答案适用于 <对象& 在一个页面上,下面是对标记的单个实例执行此操作的方法:

    // create the appropriate <param>
    var elementToAppend = document.createElement('param');
    elementToAppend.setAttribute('name', 'src');
    elementToAppend.setAttribute('value', 'blah.midi');
    // get a reference to the <object>
    var obj = document.getElementById('yourMidiPlayerObject');
    // duplicate the <object>
    var newObj = obj.cloneNode(true);
    // append the <param> to the <object>
    newObj.appendChild(elementToAppend);
    // replace the <object> on the page
    obj.parentNode.replaceChild(newObj, obj);
    

    如果 <PARAM-GT; 要修改已经存在的内容有点不同:

    // get a reference to the <object>
    var obj = document.getElementById('yourMidiPlayerObject');
    // duplicate the <object>
    var newObj = obj.cloneNode(true);
    // get a reference to the current <param>
    var param = document.getElementById('theParamId');
    // duplicate the <param>
    var newParam = param.cloneNode(true);
    // change the value of the <param>
    newParam.setAttribute('value', 'newblah.midi');
    // replace the <param> tag on the <object>
    newObj.replaceChild(newParam, param);
    // replace the <object> on the page
    obj.parentNode.replaceChild(newObj, obj);
    

    在这两种情况下,让它在IE、Opera、Safari和Chrome中工作的诀窍是替换 <对象& 在页面上。追加新的 <PARAM-GT; 现有的 <对象& 似乎不会使它重新解析值或加载新引用的内容。