JavaScript:更改嵌入标签的 src 属性

发布于 2024-08-25 16:55:22 字数 738 浏览 4 评论 0原文

我有以下场景。

我向用户展示了来自服务器的一些音频文件。用户单击其中一个,然后最终对选定的文件夹和文件执行 onFileSelected。该函数的作用是更改嵌入对象的源。因此,在某种程度上,它是在接受所选文件并保存用户的选择之前对其进行的预览。 视觉辅助

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);
};

现在,这在 Firefox 中工作得很好,但 Safari 和 Chrome 根本拒绝更改源代码,无论操作系统如何。

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

为什么 Safari 阻止我更改 源以及如何规避此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流星番茄 2024-09-01 16:55:22

您应该删除 embed 元素,并使用新的 src 参数集重新注入它。

embedobject 类似,是两个元素,由于它们的特殊用途(视频、音频、flash、activex...),在某些浏览器中的处理方式与一个普通的 DOM 元素。因此,更改 src 属性可能不会触发您期望的操作。

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

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

嘿看小鸭子会跑 2024-09-01 16:55:22

当我想更改“embed”元素的“src”属性时,我也遇到了同样的问题,所以我所做的如下:

var parent = $('embed#audio_file').parent();
var newElement = "<embed src='new src' id='audio_file'>";

$('embed#audio_file').remove();
parent.append(newElement);

这将在我的应用程序中正常工作。

结论: - 您需要首先删除嵌入元素,然后必须通过更改 src 重新插入它。

I was also facing same issue when I want to change "src"-attribute of "embed" element, so what I did, is given below:

var parent = $('embed#audio_file').parent();
var newElement = "<embed src='new src' id='audio_file'>";

$('embed#audio_file').remove();
parent.append(newElement);

And this will work fine in my application.

Conclusion: - You need to first remove the embed element and then you have to reinsert it with change in src.

策马西风 2024-09-01 16:55:22

这是 Chrome 中的一个错误。替代解决方案是不要更改嵌入元素的 src,而是尝试用新的 src 替换克隆的嵌入

$('embed').replaceWith($('embed').clone().attr('src','anotherfile.swf'));

This is a bug in Chrome. Alternative solution is instead of changing src of embed element, try replacing cloned embed with new src

$('embed').replaceWith($('embed').clone().attr('src','anotherfile.swf'));
£冰雨忧蓝° 2024-09-01 16:55:22

Chrome 中存在一个错误,请给它加星号以便尽快修复: http://code.google.com/p/chromium/issues/detail?id=69648

There is a bug in Chrome, give it a star to have it fixed sooner: http://code.google.com/p/chromium/issues/detail?id=69648

居里长安 2024-09-01 16:55:22

将 div 添加到嵌入标签,

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

在脚本中:

            var pdfId = document.getElementById("pdfId");
            pdfId.removeChild(pdfId.childNodes[0]);
            var embed = document.createElement('embed');
            embed.setAttribute('src', embedUrl);
            embed.setAttribute('type', 'audio/mpeg');
            pdfId.appendChild(embed);

Add div to embed tag,

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

In script:

            var pdfId = document.getElementById("pdfId");
            pdfId.removeChild(pdfId.childNodes[0]);
            var embed = document.createElement('embed');
            embed.setAttribute('src', embedUrl);
            embed.setAttribute('type', 'audio/mpeg');
            pdfId.appendChild(embed);
怀中猫帐中妖 2024-09-01 16:55:22

JQuery 遵循 CSS 式声明:

而不是

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

这样做。

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

jQuery 只检索 id="audio_file" 的对象,

JQuery follows the CSS-esque declaration:

Instead of doing

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

Rather do

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

That way, jQuery only retrieves object of id="audio_file".

愁杀 2024-09-01 16:55:22
var element = document.getElementById('element-embed');
changeSrcEmbed(element,'https://coccoc.com');
function changeSrcEmbed(element, src) {
    var id = element.id;
    element.src = src;
    var embedOld = document.getElementById(id);
    var parent = embedOld.parentElement;
    var newElement = element;
    document.getElementById(id).remove();
    parent.append(newElement);
}
<embed id="element-embed" style="width:1100px; height: 700px;">

var element = document.getElementById('element-embed');
changeSrcEmbed(element,'https://coccoc.com');
function changeSrcEmbed(element, src) {
    var id = element.id;
    element.src = src;
    var embedOld = document.getElementById(id);
    var parent = embedOld.parentElement;
    var newElement = element;
    document.getElementById(id).remove();
    parent.append(newElement);
}
<embed id="element-embed" style="width:1100px; height: 700px;">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文