切换 CSS Javascript 函数
我试图在第一次单击时显示此 CSS,并在下一次单击时将其删除,但toggleClass 似乎不起作用,下面的代码也不起作用。我所能得到的只是要显示的 CSS,而且它永远不会被删除。这适用于 iOS 上的 Safari。感谢您的帮助!
var menu_enabled = false;
$('#nav a').click(function () {
if (menu_enabled == true) {
$(this).removeClass('sf-js-enabled');
var menu_enabled = false;
}
else {
var menu_enabled = true;
$(this).addClass('sf-js-enabled');
}
});
I'm trying to show this CSS on the first click and remove it on the next but toggleClass doesnt seem to work and neither does my code below. All I can get is the CSS to show and it never gets removed. This is for use on Safari on iOS. Thanks for the help!
var menu_enabled = false;
$('#nav a').click(function () {
if (menu_enabled == true) {
$(this).removeClass('sf-js-enabled');
var menu_enabled = false;
}
else {
var menu_enabled = true;
$(this).addClass('sf-js-enabled');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看
.toggleClass()
,看起来这正是你想要的。You should checkout
.toggleClass()
, looks like it's exactly what you're wanting.也许使用 .className 属性?
Perhaps using the .className attribute?
我不太确定您要总体完成什么,但 addClass 和 removeClass 应该可以正常工作。我会向您想要附加点击的元素添加一个 ID 或类,但这只是个人偏好。
I'm not exactly sure what you're trying to accomplish overall, but addClass and removeClass should work just fine. I would add an ID or class to the element that you want to have the click attached to but that's just a personal preference.