繁体   English   中英

Javascript 用 if 语句注入 HTML

[英]Javascript to inject HTML with if statement

我正在尝试创建一个 function 通过 javascript 注入显示和隐藏一个 div。 function 在没有 if(条件)的情况下完美注入: $("#video-container").load("include/video.html"); 但未能正确注入 if 语句。 if 语句如何破坏这样的事情?

var show_video_feed_bool = false;
function show_video_feed() {
    if (show_video_feed_bool == false) {
        $("#video-container").load("include/video.html");
        show_video_feed_bool = true;
    }
    if (show_video_feed_bool) {
        document.getElementById("video-container").innerHTML = "";
        show_video_feed_bool = false;
    }
}

视频.html

<div id="container">
    <video autoplay="true" id="videoElement">

    </video>
</div>
<script>
    var video = document.querySelector("#videoElement");

    if (navigator.mediaDevices.getUserMedia) {
        navigator.mediaDevices.getUserMedia({ video: true })
            .then(function (stream) {
                video.srcObject = stream;
            })
            .catch(function (err0r) {
                console.log("Something went wrong!");
            });
    }
</script>

尝试:

show_video_feed_bool = false;
function show_video_feed() {
    if (!show_video_feed_bool) {
        $("#video-container").load("include/video.html");
    } else {
        document.getElementById("video-container").innerHTML = "";
    }
    show_video_feed_bool = !show_video_feed_bool;
}

暂无
暂无

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

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