gpt4 book ai didi

javascript - 如果div有class变化内容——三种情况

转载 作者:行者123 更新时间:2023-11-29 14:51:59 25 4
gpt4 key购买 nike

对于一个 div,我有三种可能的情况——它要么有一个类 .first 或 .last,要么没有。

我需要根据这些类将内容更改为另一个 div,但我做错了......

有人可以帮我看看我做错了什么吗?

jQuery(document).ready(function ()  {

if (jQuery('.pricing-plan').hasClass('first')) {
jQuery('.tagline').text('primeiro');
}
else if (jQuery('.pricing-plan').hasClass('last')) {
jQuery('.tagline').text('ultimo');
}
else {
jQuery('.tagline').text('nada');
}

});

最佳答案

Updated Demo

  • 您有多个类为 .pricing-plan.tagline 的元素。在这种情况下,您必须使用 .each() 遍历每个元素。
  • 在每个内部,要获取当前 .pricing-plan 的上下文,请使用 $(this)

  • 在您当前的代码中,选择器 jQuery('.pricing-plan') 只是首先返回/检查 .pricing-plan


检查这个,

jQuery(document).ready(function () {
jQuery('.pricing-plan').each(function () {

if (jQuery(this).hasClass('first')) {
jQuery(this).find('.tagline').text('primeiro');
} else if (jQuery(this).hasClass('last')) {
jQuery(this).find('.tagline').text('ultimo');
} else {
jQuery(this).find('.tagline').text('nada');
}
});

});

关于javascript - 如果div有class变化内容——三种情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305801/

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