我正在使用引導程序list-group
,並且列表項有兩個類。我正在使用JavaScript,並且當我只有一個類video
時它工作正常,但當我添加list-group-item
類時,它不起作用。JavaScript不適用於具有兩個類的元素
<html>
<head></head>
<body>
<div class="list-group list-special">
<a class="video list-group-item active" href="M0mx8S05v60"><span class="badge">8:56</span>Lec 1: What is Signal?</a>
<a class="video list-group-item" href="F5h3z8p9dPg"><span class="badge">9:35</span>Lec 2: What is an Analog Signal?</a>
<a class="video list-group-item" href="jRL9ag3riJY"><span class="badge">8:48</span>Lec 3: What is Digital Signal?</a>
<a class="video list-group-item" href="izBaDRyqnBk"><span class="badge">5:13</span>Lec 4: Need of Digital Signal</a>
<a class="video list-group-item " href="2xXErGeeb_Q"><span class="badge">6:42</span>Lec 5: Inroduction to Digital Electronics</a>
<a class="video list-group-item" href="RF9I6UzI4Rc"><span class="badge">9:41</span>Lec 6: Switch and Bits Intuition</a>
</div>
<script type="text/javascript">
var onVideoLinkClick = function(e){
if(e.target.tagName == 'A' && e.target.classList.contains('video')) {
var videoID = e.target.getAttribute('href');
history.pushState(
{videoID: videoID},
e.target.innerText,
'video-'+videoID)
changeVideo(videoID)
e.preventDefault();
}
}
var changeVideo = function(videoID){
var src = 'https://www.youtube.com/embed/'+videoID;
document.getElementById("videoFrame").src = src;
}
var onUrlChange = function(e){
changeVideo(e.state.videoID)
}
window.addEventListener('click', onVideoLinkClick);
window.onpopstate = onUrlChange;
</script>
</body>
</html>
爲什麼不簡單地將完整的url放在href屬性中? – jeff
請注意,* innerText *是一個專有的IE屬性,所有使用的瀏覽器都不支持。 – RobG
我對'.contains()'函數感到懷疑,你的意思是https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes? –