jQuery选择

Posted 海阔天空XM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery选择相关的知识,希望对你有一定的参考价值。

//js判断有没有元素

if(document.getElementById("tt")){
  alert("有这个元素!");
}
//jQuery判断有没有元素

if($("#tt").length > 0){
 alert("有这个元素");
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function(){//页面所有元素加载完毕
	var btn = document.getElementById("btn");  //获取id为btn的元素(button)
	btn.onclick = function(){				   //给元素添加onclick事件
		var arrays = new Array();              //创建一个数组对象
		var items = document.getElementsByName("check");  //获取name为check的一组元素(checkbox)
		for(i=0; i < items.length; i++){  //循环这组数据
			if(items[i].checked){      //判断是否选中
				arrays.push(items[i].value);  //把符合条件的 添加到数组中. push()是javascript数组中的方法.
			}
		}
		alert( "选中的个数为:"+arrays.length  );
	}
}
</script>
</head>
<body>
<form method="post" action="#">
	<input type="checkbox" value="1" name="check" checked="checked"/>
	<input type="checkbox" value="2" name="check" />
	<input type="checkbox" value="3" name="check" checked="checked"/>
	<input type="button" value="你选中的个数" id="btn"/>
</form>
</body>
</html>

  

以上是关于jQuery选择的主要内容,如果未能解决你的问题,请参考以下文章

jQuery应用 代码片段

Visual Studio 2012-2019的130多个jQuery代码片段。

markdown 在WordPress中使用jQuery代码片段

使用 NodeJS 和 JSDOM/jQuery 从代码片段构建 PHP 页面

很实用的JQuery代码片段(转)

几个有用的JavaScript/jQuery代码片段(转)