简体   繁体   English

设置多个具有相同ID的div的CSS

[英]Set css of multiple divs with same id

I have a reveal presentation in an iframe. 我在iframe中有一个精彩的演讲。 Each slide has a div with an audio player in in and the divs id is "narration". 每个幻灯片都有一个带有音频播放器的div,且div ID为“旁白”。

I have a button outside the frame that is used to hide/show this div. 我在框外有一个按钮,用于隐藏/显示此div。 The problem is that it only does this for the first slide and not the rest. 问题在于它仅对第一张幻灯片执行此操作,而对其余幻灯片不执行此操作。

EDIT : This seems to hide the divs : 编辑:这似乎隐藏了div:

function checkAudio() {
        if (document.getElementById('cb1').checked) {
var y = document.getElementById('ppt').contentWindow.document.getElementsByClassName('narration');
var i;
for (i = 0; i < y.length; i++) {
  y[i].style.display = 'none';
}
        } else {
var y = document.getElementById('ppt').contentWindow.document.getElementsByClassName('narration');
var i;
for (i = 0; i < y.length; i++) {
  y[i].style.display = 'block';
        }
    }
 }

HTML in iframe (There is one for each slide) : iframe中的HTML(每张幻灯片都有一个):

<div id="narration"><p align="middle">
    <audio controls="" preload="none">
        <source src="mp3/2.mp3" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio></p>
</div>

JS (outside of iframe): JS(在iframe之外):

 function checkAudio() {
     if (document.getElementById('cb1').checked) {
         document.getElementById('ppt').contentWindow.document.getElementById('narration').style.display = 'none';
     } else {
         document.getElementById('ppt').contentWindow.document.getElementById('narration').style.display = 'block';
     }
 }

If you want to show a specific element using a button, you should use a specific ID. 如果要使用按钮显示特定元素,则应使用特定ID。 If you want to show all items using a single button you should use classes. 如果要使用一个按钮显示所有项目,则应使用类。 You could also use classes to show a specific element eg: The 5th button will show the 5th element but this is not a good style. 您也可以使用类来显示特定元素,例如:第5个按钮将显示第5个元素,但这不是一个好的样式。

When the site in the iframe loads the next frame, your code doesn't know to hide the div it presents again. 当iframe中的网站加载下一帧时,您的代码不知道要隐藏它再次显示的div。 You need an event to process on. 您需要处理一个事件。

You need to poll the id so that if it shows up again, you can hide it. 您需要轮询ID,以便再次显示该ID时可以将其隐藏。 See: iframe contents change event? 请参阅: iframe内容更改事件?

After changing your IDs to classes ( read here why ), you need to update your javascript code to handle the multiple divs via a foreach loop. 将ID更改为类之后(请阅读此处的原因 ),您需要更新JavaScript代码以通过foreach循环处理多个div。

function checkAudio() {
    var narrationDivs = document.getElementById('ppt').contentWindow.document.getElementsByClassName('narration');
    var newDisplay = "block";

    if (document.getElementById('cb1').checked) {
        newDisplay = "none";
    }

    narrationDivs.forEach(function(div) {
        div.style.display = newDisplay;
    });
}

In order to have the code run again when your iframe changes, you need to update your iframe changing function: 为了使代码在iframe更改时再次运行,您需要更新iframe更改功能:

function setURL(url){ 
    document.getElementById('ppt').src = url; 
    checkAudio(); // Just run the function again!
}
 function checkAudio() {
        if (document.getElementById('cb1').checked) {
var y = document.getElementById('ppt').contentWindow.document.getElementsByClassName('narration');
var i;
for (i = 0; i < y.length; i++) {
  y[i].style.display = 'none';
}
        } else {
var y = document.getElementById('ppt').contentWindow.document.getElementsByClassName('narration');
var i;
for (i = 0; i < y.length; i++) {
  y[i].style.display = 'block';
        }
    }
 }

You might also want to check out the audio-slideshow plugin that allows to play separate audio files for each slide and fragment. 您可能还想查看audio-slideshow插件,该插件可为每个幻灯片和片段播放单独的音频文件。 If your main need is this, the plugin should do the job for you. 如果您的主要需求是此,则插件应为您完成这项工作。

You can find a demo here and the plugin here . 你可以找到一个演示在这里和插件在这里 There is also the slideshow-recorder plugin that allows you to record your narration. 还有一个slideshow-recorder插件,可让您录制旁白。

Asvin 阿斯文

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

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