[英]Removing a class by selecting the element through multiple classes and a method/selector
I have searched multiple different responses for similar issues - not the same and have tried a few different possibilities. 对于相似的问题,我已经搜索了多个不同的答案-不一样,并尝试了几种不同的可能性。 (Links will be provided for what I've tried) (将提供我尝试过的链接)
Context: 语境:
This is a personal project and for the purpose of this task, I am not allowed to touch the HTML code. 这是一个个人项目,出于此任务的目的,我不允许触摸HTML代码。 I am trying to remove a class using JQuery to select its parent class with a selector / method :last-of-type
or .last()
. 我正在尝试使用选择器 / 方法 :last-of-type
或.last()
选择使用JQuery来选择其父类的类。
Here is what I have already tried: 这是我已经尝试过的:
HTML: HTML:
<fieldset>
<div class="form-group">
<div class="col-sm-8 col-sm-12">
<input class="form-control" id="Username" name="Username" title="Username is a required field." type="text" value="" placeholder="Username">
</div>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-12">
<input autocomplete="off" class="form-control" id="Password" name="Password" title="Password is a required field." type="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8 col-sm-12">
<div class="checkbox">
<label>
<input id="RememberMe" name="RememberMe" type="checkbox" value="true">
<span>Remember me?</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8 col-sm-12">
<button id="submit-signin-local" class="btn btn-primary" title="Sign In">Sign In</button>
<a class="btn btn-default" role="button" href="/ForgotPassword" title="Forgot Password?">Forgot Your Password?</a>
</div>
</div>
</fieldset>
and JQuery: 和JQuery:
var lastForm = $('.form-group').last();
$(lastForm+' div.col-sm-12').removeClass('col-sm-offset-4');
The result from that code, is no changes to the HTML... The class still exists and is not removed. 该代码的结果是,没有对HTML进行任何更改...该类仍然存在,并且不会被删除。 I believe the issue here is to do with the selector / method . 我相信这里的问题与选择器 / 方法有关 。 I originally tried 我最初尝试过
$('.form-group div.col-sm-12').removeClass('col-sm-offset-4');
but looking at a bunch of StackOverflow responses, it didn't look possible to do so. 但是看一堆StackOverflow响应,似乎没有可能。 jQuery selector with nth-of-type() 具有nth-of-type()的jQuery选择器
I may be searching the wrong thing, so it is possible it is a duplicate - but after hours of searching - I doubt it 我可能正在搜索错误的内容,因此可能是重复的内容-但是经过数小时的搜索-我对此表示怀疑
CodePen for anyone who wants to try - https://codepen.io/pen/ERjYXO 想要尝试的人可以使用CodePen- https: //codepen.io/pen/ERjYXO
Can you try this 你可以试试这个吗
var lastForm = $('.form-group').last();
$('div.col-sm-12', lastForm).removeClass('col-sm-offset-4');
this is my code with your given html 这是我的代码与您给定的html
<script type="text/javascript">
var lastForm = jQuery('.form-group').last();
console.log(lastForm);
jQuery('div.col-sm-12', lastForm).removeClass('col-sm-offset-4');
</script>
You can use find
method and select direct child > div
of your last element. 您可以使用find
方法并选择最后一个元素的直接子代> div
。 DEMO DEMO
var lastForm=$('.form-group').last();
lastForm.find("> div").removeClass('col-sm-offset-4');
You could also use selector like this 您也可以像这样使用选择器
$(".form-group:last > div").removeClass('col-sm-offset-4');
Use this script 使用此脚本
$("#submit-signin-local").parent().removeClass('col-sm-offset-4');
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <fieldset> <div class="form-group"> <div class="col-sm-8 col-sm-12"> <input class="form-control" id="Username" name="Username" title="Username is a required field." type="text" value="" placeholder="Username"> </div> </div> <div class="form-group"> <div class="col-sm-8 col-sm-12"> <input autocomplete="off" class="form-control" id="Password" name="Password" title="Password is a required field." type="password" placeholder="Password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-4 col-sm-8 col-sm-12"> <div class="checkbox"> <label> <input id="RememberMe" name="RememberMe" type="checkbox" value="true"> <span>Remember me?</span> </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-4 col-sm-8 col-sm-12"> <button id="submit-signin-local" class="btn btn-primary" title="Sign In">Sign In</button> <a class="btn btn-default" role="button" href="/ForgotPassword" title="Forgot Password?">Forgot Password?</a> </div> </div> </fieldset> <script> $("#submit-signin-local").parent().removeClass('col-sm-offset-4'); </script>
Can you please try this? 你可以试试看吗?
var lastForm = $('.form-group').last();
$(lastForm).find(".col-sm-offset-4").removeClass("col-sm-12");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.