用jquery选择器写的怎么不行?
/** * Created by ycx on 2017/4/6. */ ;(function () { 'use strict'; var $oDiv = $('.block'); console.log($oDiv); $oDiv.onmouseover = function () { startMove(0); } $oDiv.onmouseout = function () { startMove(-200); } var timer = null; function startMove(target) { clearInterval(timer); var $oDiv = $('.block'); var speed = 0; if ($oDiv.offsetLeft > target) { speed = -10; } else { speed = 10; } timer = setInterval(function () { if ($oDiv.offsetLeft == target) { clearInterval(timer); } else { $oDiv.style.left = $oDiv.offsetLeft + speed + 'px'; } }, 30) } })();
在选择器var $oDiv = $('.block');这里就报错没有$()这个方法,怀疑是引用的问题,但是不管是npm安装jquery,还是直接引用cdn,都不行,到底是什么原因啊??
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="base.js"></script>
<script src="node_modules/jquery/dist/jquery.js"></script> <script src="base.js"></script>