2016-01-27 161 views
-3

看来,当我将它放入与<script></script>的HTML中时,JavaScript不会运行,但它对于在线工具正常工作。当图像被点击时,JavaScript应该能够用视频替换图像。我怎样才能添加到HTML?HTML不能与JavaScript一起工作

它在使用这个在线工具 http://jsfiddle.net/odoycz6n/3/

HTML

<img src="http://www.whatever9495.com/template/damei_d21/image/logo.png" data-video="https://drive.google.com/file/d/0Bwbk-P9knHkGa21vSzYzMFl6YXM/preview"> 

的JavaScript

$('img').click(function() { 
    video = '<iframe src="' + $(this).attr('data-video') + '" width="715" height="480" frameborder="0" allowfullscreen=""></iframe>'; 
    $(this).replaceWith(video); 
}); 
+2

你是否包括jQuery的?你如何将你的脚本添加到你的HTML? –

+2

java与JavaScript无关。 – epascarello

+3

我的猜测,如果你不像小提琴那样加载你的代码。 – epascarello

回答

0

的jsfiddle负荷的jQuery默认情况下正常工作。你需要在你自己的代码中使用它之前加载的jQuery像

<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script> 
+1

不,jsFiddle默认不加载jQuery或任何库。但是,它默认将JavaScript代码加载到'window.onload'事件中。 – j08691

+1

jsfiddel默认不加载任何库 – charlietfl

0

只需插入<script>下面<img >

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
</head> 
<body> 
<img src="http://www.whatever9495.com/template/damei_d21/image/logo.png" data-video="https://drive.google.com/file/d/0Bwbk-P9knHkGa21vSzYzMFl6YXM/preview"> 
<script> 
$('img').click(function() { 
video = '<iframe src="' + $(this).attr('data-video') + '" width="715" height="480" frameborder="0" allowfullscreen=""></iframe>'; 
$(this).replaceWith(video); 
}); 
</script> 
</body> 
</html>