简体   繁体   English

在IE中,脚本不会更改div的innerhtml

[英]In IE, script doesn't change innerhtml of div

I'm making a function that changes the source of an embed tag upon clicking the title of a song. 我正在制作一个函数,可以在单击歌曲的标题时更改embed标签的来源。 This works in Chrome and Firefox, but not in Internet Explorer. 这在Chrome和Firefox中有效,但在Internet Explorer中无效。

Here is the function: 这是函数:

function loadSong(title) {
    var wav = "samples/" + title + ".wav";
    var ie = "<embed id='ie' src='" + wav + "'></embed>";
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { 
        //test for MSIE x.x
        var newSong = document.getElementById("playerwrapper")
        newSong.innerHTML = ie;
    } else {
        var audio = document.getElementById("audio");  
        audio.pause();
        audio.src = wav;
        audio.play();
    }
}

And yes, I tried the embedded player in Chrome and Firefox, and I tried using the html5 audio element in IE with m4a files (with the source being changed in the same way as in Chrome and firefox). 是的,我尝试在Chrome和Firefox中使用嵌入式播放器,并尝试在IE中将html5音频元素与m4a文件一起使用(源的更改方式与在Chrome和firefox中相同)。

Next, this is the audio player itself (including the whole thing just in case; sorry if it's irrelevant): 接下来,这是音频播放器本身(包括整个内容,以防万一;如果不重要,请对不起):

<?php
if($samples == 1) {
    if (isset($_SERVER['HTTP_USER_AGENT']) && 
        (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) 
    {
        print "
            <div id='playrwrapper'>
                <embed id='ie' src='samples/alright.wav' autostart='false'>
                </embed>
            </div>";
    } else {
        print "
            <div id='playerwrapper'>
                <audio id='audio' preload='auto' tabindex='0' controls>
                    <source id='wav' type='audio/wav' src='samples/alright.wav'/>
                </audio>
            </div>";
    };
};
?>

Finally, this is how the function is being called: 最后,这是函数的调用方式:

<p onclick='loadSong("songtitle")'>Song Title</p>
<p onclick='loadSong("anothersongtitle")'>Another Song Title</p>
<p onclick='loadSong("doomkittiesofmars")'>Doom Kitties of Mars</p>

Thanks in advance for any advice you have. 预先感谢您的任何建议。

IE's a bitch, isn't he? IE是个a子,不是吗?

IE doesn't reload an object element if its source has changed. 如果源更改,IE不会重新加载object元素。 It only loads it when the page loads, period. 仅在页面加载期间加载它。

So how to work around that? 那么如何解决呢? Simple: clone the existing element, delete the old one, and append the new one. 简单:克隆现有元素,删除旧元素,然后追加新元素。 Example: 例:

// innerHTML bla bla bla
newS = newSong.cloneNode( true ) // Deep copy
newSong.parentNode.replaceChild( newS, newSong )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM