gpt4 book ai didi

jquery - onClick 从 div 中删除类

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:41 25 4
gpt4 key购买 nike

html

    <div class="perWatchVideo" id="remove">
<div class="controls">
<input type="link" id="link" name="link" placeholder="Place Link here" class="linkInput">
<button class="btn btn-primary loadVideo">Load Video!</button>
</div>
</div>
jquery
$(document).ready(function(){
$("loadVideo").click(function(){
$("#remove").removeClass("preWachVideo");
});
});

在 div 标签内,我创建了一个名为 perWatchVideo 的类。当我单击按钮时,我想摆脱这个类并将其替换为另一个类 postWatchVideo 类。我如何在 jQuery 中执行此操作?

最佳答案

首先你忘记了 .loadVideo 之前是 $(".loadVideo") 而不是 $("loadVideo ")

你需要使用.addClass()

$(document).ready(function(){
$(".loadVideo").click(function(){
$("#remove").removeClass("preWachVideo").addClass('postWatchVideo');
});
});

或者如果您需要在每次点击时切换类,您可以使用 toggleClass();

$(document).ready(function(){
$(".loadVideo").click(function(){
$("#remove").toggleClass("preWachVideo postWatchVideo");
});
});

Note: ID suppose to be unique If you have more than one element with id="remove" your code will work just with the first #remove element

所以你需要改变它

<div class="perWatchVideo" id="remove">

<div class="perWatchVideo remove">

那么你的代码应该是这样的

$(document).ready(function(){
$(".loadVideo").click(function(){
$(this).closest(".remove").removeClass("preWachVideo").addClass('postWatchVideo');
});
});

关于jquery - onClick 从 div 中删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43955100/

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