HTML 不能与 JavaScript 一起使用

HTML not working with JavaScript

本文关键字:一起 JavaScript 不能 HTML      更新时间:2023-09-26

当我用<script></script>将JavaScript放入html中时,JavaScript似乎无法运行,但是它与在线工具一起使用可以正常工作。他们JavaScript应该能够在点击图像时用视频替换图像。如何添加到 html 中?

使用此在线工具时可以正常工作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);
});

jsfiddle 默认加载 jQuery。在您自己的代码中使用它之前,您需要加载 jQuery,例如

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

只需在下面插入<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>