分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

jQuery常用方法

发布时间:2023-09-06 01:14责任编辑:胡小海关键词:jQuery

0、常用代码:

(1)AJAX请求

$(function() {$(‘#send‘).click(function() {$.ajax({type: "GET", //GET或POST,async:true, //默认设置为true,所有请求均为异步请求。url: "http://www.idaima.com/xxxxx.php",data: {username: $("#username").val(),content: $("#content").val()},dataType: "json", //xml、html、script、jsonp、textbeforeSend:function(){},complete:function(){},success: function(data) {alert(data)},error:function(){},});});});

  

(2) 如何获取checkbox,并判断是否选中

$("input[type=‘checkbox‘]").is(‘:checked‘) //返回结果:选中=true,未选中=false

(3) 获取checkbox选中的值

var chk_value =[]; $(‘input[name="test"]:checked‘).each(function(){ ????chk_value.push($(this).val()); });

(4)checkbox全选/反选/选择奇数

$("document").ready(function() { ???$("#btn1").click(function() { ???????$("[name=‘checkbox‘]").attr("checked", ‘true‘); //全选 ????}) $("#btn2").click(function() { ???????$("[name=‘checkbox‘]").removeAttr("checked"); //取消全选 ????}) $("#btn3").click(function() { ???????$("[name=‘checkbox‘]:even").attr("checked", ‘true‘); //选中所有奇数 ????}) $("#btn4").click(function() { ???????$("[name=‘checkbox‘]").each(function() { //反选 ????????????if ($(this).attr("checked")) { ???????????????$(this).removeAttr("checked"); ???????????} else { ???????????????$(this).attr("checked", ‘true‘); ???????????} ???????}) ???})})

(5)获取select下拉框的值

其实下拉框的最简单了,直接val就行了

$("#select").val()

(6)获取选中值,三种方法都可以:

$(‘input:radio:checked‘).val();$("input[type=‘radio‘]:checked").val();$("input[name=‘rd‘]:checked").val();

(7)设置第一个Radio为选中值 , 两种方法都可以:

$(‘input:radio:first‘).attr(‘checked‘, ‘checked‘);$(‘input:radio:first‘).attr(‘checked‘, ‘true‘);

(8)设置最后一个Radio为选中值:

$(‘input:radio:last‘).attr(‘checked‘, ‘checked‘);$(‘input:radio:last‘).attr(‘checked‘, ‘true‘);

(9)根据索引值设置任意一个radio为选中值:

$(‘input:radio‘).eq(索引值).attr(‘checked‘, ‘true‘);//索引值=0,1,2....$(‘input:radio‘).slice(1,2).attr(‘checked‘, ‘true‘);

(10)根据Value值设置Radio为选中值

$("input:radio[value=‘rd2‘]").attr(‘checked‘,‘true‘);$("input[value=‘rd2‘]").attr(‘checked‘,‘true‘);

1、关于页面元素的引用

通过jquery的$()引用元素包括通过id、class、元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom定义的方法。

2、jQuery对象与dom对象的转换

只有jquery对象才能使用jquery定义的方法。注意dom对象和jquery对象是有区别的,调用方法时要注意操作的是dom对象还是jquery对象。

普通的dom对象一般可以通过$()转换成jquery对象。 

如:

$(document.getElementById("msg"))

则为jquery对象,可以使用jquery的方法。

由于jquery对象本身是一个集合。所以如果jquery对象要转换为dom对象则必须取出其中的某一项,一般可通过索引取出。

如:

$("#msg")[0],$("div").eq(1)[0],$("div").get()[1],$("td")[5]

这些都是dom对象,可以使用dom中的方法,但不能再使用Jquery的方法。 

以下几种写法都是正确的:

$("#msg").html();$("#msg")[0].innerHTML;$("#msg").eq(0)[0].innerHTML;$("#msg").get(0).innerHTML;

3、如何获取jQuery集合的某一项

对于获取的元素集合,获取其中的某一项(通过索引指定)可以使用eq或get(n)方法或者索引号获取,要注意,eq返回的是jquery对象,而get(n)和索引返回的是dom元素对象。对于jquery对象只能使用jquery的方法,而dom对象只能使用dom的方法,如要获取第三个<div>元素的内容。有如下两种方法: 

$("div").eq(2).html(); //调用jquery对象的方法$("div").get(2).innerHTML; //调用dom的方法属性

jQuery常用方法

原文地址:http://www.cnblogs.com/yinyl/p/7610959.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved