我想做的是在视频下面放一些控件。在很多地方我都需要这些控件,所以我决定使用React来渲染它们,以便它们易于重用。
这是我的代码:
import React from 'react';
import ReactDOM from 'react-dom';
import './styles.css';
class VideoControls extends React.Component {
render() {
return(
<div>TESTING</div>
);
}
}
ReactDOM.render(
<VideoControls />,
document.getElementById('controls')
);
/* code for tags */
html, body {
height: 100%;
}
html {
background-color: rgb(48, 48, 48);
}
body {
padding: 16px;
/* overrides default w3.css */
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
}
video {
display: block;
}
/* specialised code for other elements */
.video-panel, .video-panel-second {
background-color: rgb(75, 75, 75);
color: rgb(185, 185, 185);
height: 45%;
width: calc(50% - 11px);
display: inline-block;
text-align: center;
}
.video-panel-second {
margin-left: 16px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css"/>
<link rel="stylesheet" href="w3.css"/>
<script src="https://unpkg.com/react@16/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js" type="text/babel"></script>
<title>Editor UI Demo</title>
</head>
<body>
<div class="video-panel w3-container w3-padding-24 w3-round">
<video id="source-video" src="/css-vse/assets/PostingLetter.MOV" height="100%" width="100%">Video not displayed.</video>
<div id="controls"></div>
</div>
<div class="video-panel-second w3-container w3-padding-24 w3-round">
<video id="timeline-video" src="/css-vse/assets/Walking.MOV" height="100%" width="100%">Video not displayed.</video>
<div id="controls"></div>
</div>
<script src="index.js"></script>
</body>
</html>
我想在<VideoControls>
中呈现一个div#controls
,但是在尝试这样做时,我总是收到“ Uncaught SyntaxError:Unexpected Identifier”。是否可以在HTML文件中使用<VideoControls>
React组件,还是必须使整个页面成为React应用?
谢谢您的时间。
答案 0 :(得分:0)
使用类代替使用id呈现Component。获取具有类名称的元素,并在循环中呈现您的react组件。
Follow this answer I found on StackOverFlow,将帮助您解决此问题。
工作示例。
<!-- Index.html -->
<div id="app">
INSIDE APP DIV
<div class="here">
</div>
<div class="here">
</div>
</div>
和React组件
class VideoControls extends React.Component {
render() {
return (
<div>My VideoControls</div>
)
}
}
// getting all the divs with class here
let myContainerLength = document.getElementsByClassName("here").length;
if (myContainerLength > 0) {
// if there are more than one instances of element with the class `here`
// loop through the elements with class `here` using and index `i`
for (let i=0; i<myContainerLength; i++) {
ReactDOM.render(<VideoControls />, document.getElementsByClassName("here")[i]);
}
}
工作示例:JSFiddle