gpt4 book ai didi

jquery - 我怎样才能缩短这个jquery代码

转载 作者:行者123 更新时间:2023-12-01 07:08:13 25 4
gpt4 key购买 nike

我正在尝试从在线教程中学习jquery。以下代码对我有用,但如何缩短和改进此代码? this can be viewed on bottom of this page

$(document).ready(function() {
$("#alltiles > div").mouseenter(function() {
$(this).addClass("tile1");
$(this).removeClass("tile");
$('img', this).addClass("tileimg1");
$('img', this).removeClass("tileimg");
$('p', this).addClass("tilep1");
$('p', this).removeClass("tilep");
});
$("#alltiles > div").mouseleave(function() {
$(this).addClass("tile");
$(this).removeClass("tile1");
$('img', this).addClass("tileimg");
$('img', this).removeClass("tileimg1");
$('p', this).addClass("tilep");
$('p', this).removeClass("tilep1");
});
});

最佳答案

您可以使用toggleClass() 悬停时:

$(function() {
$("#alltiles > div").hover(function() {
$(this).toggleClass("tile","tile1");
$('img', this).toggleClass("tileimg","tileimg1");
$('p', this).toggleClass("tilep","tilep1");
});
});

关于jquery - 我怎样才能缩短这个jquery代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com