JQuery中怎么设置class

JQuery中怎么设置class

Jquery

JQuery中设置class相关的有如下三个方法:

$(selector).addClass();       // 向被选元素添加一个或多个类$(selector).removeClass();    // 从被选元素删除一个或多个类$(selector).toggleClass();    // 对被选元素进行添加/删除类的切换操作

下面实例演示一下以上三个方法的用法:

  1. 创建Html元素

    <div class="box">	<span>点击不同按钮后,观察效果:</span><br>	<div class="content">		<li>list-item-1</li>		<li>list-item-2</li>		<li>list-item-3</li>		<li>list-item-4</li>		<li>list-item-5</li>	</div>			<input type="button" class="btn1" value="偶数位置添加红色样式"><br>	<input type="button" class="btn2" value="交换红色、黑色样式"><br>	<input type="button" class="btn3" value="去除所有红色样式"></div>
  2. 简单设置css样式
    div.box{width:300px;height:300px;padding:10px 20px;border:4px dashed #ccc;}div.box>span{color:#999;font-style:italic;}div.content{width:250px;height:100px;margin:10px 0;padding:5px 20px;border:1px solid green;}input{margin:10px;}input[type=‘button‘]{width:200px;height:35px;margin:10px;border:2px solid #ebbcbe;}.red{color:red;}
  3. 编写jquery代码
    $(function(){	$("input:button.btn1").click(function() {		$(".content li:odd").addClass(‘red‘);	});	$("input:button.btn2").click(function() {		$(".content li").toggleClass(‘red‘);	});	$("input:button.btn3").click(function() {		$(".content li").removeClass(‘red‘);	});})
  4. 观察显示效果
  • 初始样式

  • 点击第一个按钮,给偶数位置的列表项目添加red类

  • 点击第二个按钮,有red类的元素去除red类,没有的就加上red类

  • 点击第三个按钮,删除所有的red类

时间: 2024-07-30 05:55:33

JQuery中怎么设置class的相关文章

解决JQuery中datatables设置隐藏显示列多次提交后台刷新数据的问题

此次项目开发过程中用到了Jquery的Datatables插件,无疑他是数据列表展示,解决MVC中同步过程中先走控制器后返回视图,查询数据过程中无法提示等待的弊端, 而且他所提供的各种方法也都有较强的实用性.但是再好的程序也会有瑕疵,项目开发中就遇到了其提供的设置隐藏显示列的方法 table.fnSetColumnVis(colShowDic_key[i], true);//colShowDic_key[i]为列的索引.会多次提交刷新数据(有多少列会提交刷新多少次). 为解决这个问题尝试了很多种

jQuery中animate设置属性的一个问题

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="../js/jquery-2.1.4.min.js"></script> <style> .bigger, .smaller { display: inlin

1. jQuery中的DOM操作

1)查找节点 通过jQuery选择器来完成 2)创建节点 创建元素节点:var newTd = $("<td></td>") 创建文本节点:在创建元素节点时直接把文本内容写出来.var newTd = $("<td>文本内容</td>") 3)插入节点: append() 在每个匹配元素里面的末尾处插入参数内容.作为它的最后一个子元素. 参数 一个或多个DOM 元素,DOM元素数组,HTML字符串,或者jQuery对象

Jquery Ajax 异步设置Table中某列的值

可根据table中某列中的ID去改变某列的值! JS: 1 $(document).ready(function () { 2 setTimeout(GetDate, 1000); 3 4 }); 5 6 function GetDate() { 7 $("#tbData tbody").find("tr").each(function () { 8 var prjectBalanceObj = $(this).find("td:eq(3)");

jQuery中使用attribute,prop获取,设置input的checked值

1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=""或 checked="checked",$(obj).prop("checked")的结果都是true: 当input控件checkbox没设置checked属性时,$(obj).prop("checked")的结果是false. 设置$("input[name='checkboxall'

jquery中的属性和样式设置

添加属性 $target.attr({"title":"one piece","name":"solgan"}); 为目标元素添加title和name属性 添加之前: <div class="second"> <p>我是路飞,要成为海贼王的男人.</p> </div> 添加之后: <div class="second"> <

jquery中使用css,offset和position设置top和left属性

有一个dom元素,它的position属性值为absolute,要通过jquery设置它的位置. 我们可以通过三种方法设置该元素的top和left值,设置这两个属性的值时,元素的position属性必须为absolute或relative. 第一种:使用jQuery对象的css方法,使用该方法可以直接把对象的位置设定到指定的地方,指定哪个位置就是哪个位置. 第二种:使用jQuery对象的offset方法,使用该方法时要注意,offset方法的作用是获取或设置匹配元素在当前视口的相对偏移.这句话粗

jQuery.ajax() 如何设置 Headers 中的 Accept 内容

其实很简单,首先如果是常见类型,则请直接设置 dataType 属性 $.ajax({ dataType: "json", type: "get", success: function (data) { } }); 设置 dataType 后,会去 accepts 属性(此属性会预置一些常用类型)中直接拿相应的类型添加到 Accept 中. 如果想自己自定义 jQuery 中没有的 Accept 内容,可以手动设置 accepts 属性,使用 键值对 存储,然后再设

jQuery中使用attribute,prop获取,设置input的checked值【转】

1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=""或 checked="checked",$(obj).prop("checked")的结果都是true: 当input控件checkbox没设置checked属性时,$(obj).prop("checked")的结果是false. 设置$("input[name='checkboxall'