JavaScript:更改嵌入标签的src属性 [英] JavaScript: Changing src-attribute of a embed-tag

查看:83
本文介绍了JavaScript:更改嵌入标签的src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况。

我向用户显示一些来自服务器的音频文件。用户点击一个,然后onFileSelected最终与选定的文件夹和文件一起执行。该函数的功能是从嵌入对象中更改源代码。因此,某种程度上,它是在接受它之前预览选定的文件并保存用户的选择。 视觉帮助



HTML

 < embed src =/ resources / audio / _webbook_0001 / embed_test.mp3type =audio / mpegid =audio_file> 

JavaScript

  function onFileSelected(file,directory){
jQuery('embed#audio_file')。attr('src','/ resources / audio /'+ directory +'/'+ file);
};

现在,这在Firefox中运行良好,但Safari和Chrome只是拒绝更改源代码,无论操作系统。



jQuery找到对象(jQuery.size()返回1),它执行代码,但HTML代码没有变化。



为什么Safari阻止我更改< embed> 来源,我该怎样绕过这个?

$ b $你应该删除嵌入的元素,并用新的 src重新注入它 参数集。



embed object 和类似的两个元素,由于他们的特殊用途(视频,音频,flash,activex等),在某些浏览器中的处理方式与正常的DOM元素不同。因此,更改src属性可能不会触发您期望的操作。



最好的方法是删除现有的 embed 对象重新插入它。如果你使用src属性作为参数编写某种包装函数,这应该很容易

I have the following scenario.

I show the user some audio files from the server. The user clicks on one, then onFileSelected is eventually executed with both the selected folder and file. What the function does is change the source from the embedded object. So in a way, it is a preview of the selected file before accepting it and save the user's choice. A visual aid.

HTML

<embed src="/resources/audio/_webbook_0001/embed_test.mp3" type="audio/mpeg" id="audio_file">

JavaScript

function onFileSelected(file, directory) {
   jQuery('embed#audio_file').attr('src', '/resources/audio/'+directory+'/'+file);
};

Now, this works fine in Firefox, but Safari and Chrome simply refuse to change the source, regardless of Operating System.

jQuery finds the object (jQuery.size() returns 1), it executes the code, but no change in the HTML Code.

Why does Safari prevent me from changing the <embed> source and how can I circumvent this?

解决方案

You should remove the embed element and reinject it with the new src parameter set.

embed like object and similar are two elements which, due do their special uses (video, audio, flash, activex, ...), in some browsers are handled differently from a normal DOM element. Thus changing the src attribute might not trigger the action you expect.

The best thing is to remove the existing embed object an reinsert it. If you write some kind of wrapper function with the src attribute as parameter this should be easy

这篇关于JavaScript:更改嵌入标签的src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆