从多个类中删除 Attr

removeAttr from multiple classes

本文关键字:删除 Attr      更新时间:2023-09-26

是否可以从多个类和 ID 中使用 removeAttr? 我找不到任何东西。

像这样的 smth $('.one.two.six.eleven#box').removeAttr("style");和这个都不起作用

$(".one.two.three#smth.seven").css("transition", "3s");

我无法设法在多个类和 id 上使用 removeAttr,这些类和 id 不是彼此:)的子级。

我也无法设法将 CSS() 添加到 Windows 中的许多免费类中,每个类都有自己的参数,但有一些绑定来处理所有这些参数。

在小提琴中,我有一些基本的工作,但对我来说太长了太多的行。 似乎很糟糕。

谢谢大家的回答。

放置JSfiddle来显示我制造的混乱,但想做"DRY"和"LIM"。

摆弄叉子...

解释的新编辑

    function some_func() {
    $(".some").removeAttr("style");
    $(".thing").css("transition", "3s");
    $(".right").removeAttr("style");
    $(".right").css("transition", "3s");
    $(".left").removeAttr("style");
    $(".left").css("transition", "3s");
    $(".hello").removeAttr("style");
    $(".top").css("transition", "3s");
    $("#car").removeAttr("style");
}

如何制作 2 行

$(".one.two.three#smth.seven")表示您正在选择一类、二类、类三类等的所有对象。 (我什至不认为三个#smth会起作用)。

例如:<div class="one two three">将由$(".one.two.three")选择

如果要将来自多个选择器的对象并集,可以逗号分隔它们:

$(".one, .two, .three, #smth, .seven")

这应该返回所有 .one 或 .two 或 .three 或 #smth 或 .seven 的对象

<div class="one"></div><div class="two"></div><div class="three"></div>都将被选中。

试试这个,

$('button').on('click',function(){
	$('.one,.three,.five').removeAttr('class');
  $('#two,#four').removeAttr('id');
});
li{
  color: green;
}
.one{
  color:red;
}
#two{
  color:blue;
}
.three{
  color:purple;
}
#four{
  color:yellow;
}
.five{
  color:black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button>click me</button>
<ul>
  <li class="one">1</li>
  <li id="two">2</li>
  <li class="three">3</li>
  <li id="four">4</li>
  <li class="five">5</li>
</ul>

如果可以为所有这些元素添加一个公共类,则可以使用该类删除属性。

例如-

目录

<div class="position right"></div>
<div class="position left"></div>
<div class="position hello"></div>
<div id="car" class="position"></div>

溶液

$('.position').removeAttr('style')

$(document).ready(function() {
  $("#buttonella").click(function() {
    $('.right').addClass('right-toggle');
    $('.right1').addClass('right1-toggle');
    $('#box').addClass('box-toggle');
  });
  $("#buttonella2").click(function() {
    $('#box').removeClass("box-toggle");
    $('.freebox').removeClass();
    $(".left").css("background", "pink");
    $('.right').removeClass('right-toggle');
  });
})
:root {
  padding: 20px;
}
[class] {
  margin-left: 10px;
  margin-top: 50px;
}
#buttonella {
  color: blue;
  font-size: 30px;
  float: left;
}
#buttonella2 {
  color: blue;
  font-size: 30px;
  float: right;
}
.box-toggle {
   background: red;
   width: 300px;
   margin: 0px;
   font-size: 20px;
   display: block;
}
.right-toggle, .right1-toggle {
   background: green;
   width: 200px;
   font-size: 30px;
   display: block;
}
.freebox {
  background: red;
  width: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="buttonella">click</div>
<div id="buttonella2">unclick</div>
<div id="box">boxboxboxbxo bxoxboxbox
  <div class="right">right rith ehiteh hereee
  </div>
  <div class="left">left left left here
  </div>
  <div class="right1">right1 class is here
  </div>
</div>
<div class="freebox">freebox class is here
</div>