简体   繁体   English

Jquery 点击事件未触发

[英]Jquery click event not triggering

I'm remaking my website and want to do everything from scratch (no bootstrap, foundation, etc).我正在重新制作我的网站,并希望从头开始做所有事情(没有引导程序、基础等)。 Right now I'm making the navbar and I'm trying to get it to toggle slide in and out on click.现在我正在制作导航栏,并试图让它在点击时切换滑入和滑出。 For some reason click events aren't working and I'm not sure what I did wrong.由于某种原因,点击事件不起作用,我不确定我做错了什么。 I made a rough example in codepen here我在这里用codepen做了一个粗略的例子

HTML

<script src="jquery-3.4.1.min.js"></script>
<div class="box"></div>

CSS

.box {
  transform: translateX(0);
  width: 100px;
  height: 100px;
  background-color: red;
  transition: transform 250ms;
}
.slide {
  transform: translateX(250px);
}

JS

$(function() {
  $('.box').on('click', function(slideToggle) {
    $(this).toggleClass('slide');
  });
});

For you codepen example:对于您的代码笔示例:

Go to settings -> Add library -> search jquery and select. Go 到设置 -> 添加库 -> 搜索 jquery 和 select。

If this is the same case with your real code:如果这与您的真实代码相同:

open console -> check if $ is not defined or $ is not jquery?打开控制台-> 检查 $ 是否未定义或 $ 不是 jquery?

if $ not defined, add a valid jquery cdn url.如果 $ 未定义,则添加有效的 jquery cdn url。

if $ is not jquery, try to find a conflicting library.如果 $ 不是 jquery,请尝试查找冲突库。

It seems to work fine here in a snippet.它似乎在一个片段中工作正常。 If you replace your script on codepen with the vanilla version below, it works there too.如果您将 codepen 上的脚本替换为下面的 vanilla 版本,它也可以在那里工作。 (Note: only tested in Chrome) (注:仅在 Chrome 中测试)

 $(function() { $('.box').on('click', function(slideToggle) { $(this).toggleClass('slide'); }); });
 .box { transform: translateX(0); width: 100px; height: 100px; background-color: red; transition: transform 250ms; }.slide { transform: translateX(250px); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="jquery-3.4.1.min.js"></script> <div class="box"></div>

Alternative script for your codepen:您的 codepen 的替代脚本:

document.getElementsByClassName("box")[0].addEventListener('click', slide);

function slide(event){
  const cl = event.target.classList;
  if(cl.contains("slide")){ cl.remove("slide"); }
  else{ cl.add("slide"); }
}

You have not loaded jQuery correctly for codepen.您尚未为 codepen 正确加载 jQuery。 To add jQuery simply replace the <script src="jquery-3.4.1.min.js"></script>要添加 jQuery 只需替换<script src="jquery-3.4.1.min.js"></script>

with

<script src="https://code.jquery.com/jquery-1.12.3.js" integrity="sha256-1XMpEtA4eKXNNpXcJ1pmMPs8JV+nwLdEqwiJeCQEkyc=" crossorigin="anonymous"></script>

You can add jquery cdn manually in setting just click the setting icon and click the javascript tab then under that search the jquery and hit enter.您可以在设置中手动添加 jquery cdn,只需单击设置图标并单击 javascript 选项卡,然后在该选项卡下搜索 jquery 并按 Enter。

screenshot for jquery cdn codepen setting jquery cdn codepen 设置的屏幕截图

Everything is good in your code except your jQuery reference, please check your downloaded jquery path, or you can use like below, now i am using jquery directly from the jquery official site Everything is good in your code except your jQuery reference, please check your downloaded jquery path, or you can use like below, now i am using jquery directly from the jquery official site

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div class="box"></div>

extra note: found you added some new CSS3 attribute ("transition: transform 250ms;"), when ever you use the CSS3 attribute please make sure you should reset the attribute for all browsers, like wekit,moz,额外说明:发现您添加了一些新的 CSS3 属性(“transition: transform 250ms;”),当您使用 CSS3 属性时,请确保您应该重置所有浏览器的属性,例如 wekit、moz、

thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM