jQuery - hasClass in if语句不能很好地运行 [英] jQuery — hasClass in if statement not playing nicely

查看:100
本文介绍了jQuery - hasClass in if语句不能很好地运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我不在这里做蠢事,但我相信我明白这个好。这是我的代码:

I hope I'm not doing something silly here but I'm sure I understand this alright. Here's my code:

<body class="someClass">Hi</body>

if($(body).hasClass("someClass")){
   alert("yo");
}

由于某种原因,做错了吗?

For some reason it just doesn't work for some reason, what am I doing wrong?

我放了一个jsfiddle它这里

I've put a jsfiddle of it here

推荐答案

Body需要是一个字符串,你可能希望代码在文档准备就绪时执行。 p>

Body needs to be a string and you probably want the code to execute when the document is ready.

$(function(){
   if ($('body').hasClass('someClass')) {
      alert('yo');
   }
});

这篇关于jQuery - hasClass in if语句不能很好地运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆