《锋利的jquery》源码整理——jquery技巧下

  接上:http://www.cnblogs.com/akou/p/4461557.html

代码:

  13.获取选中的下拉框

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../scripts/jquery.js" type="text/javascript"></script>

</head>
<body>
<input type="button" id="send1" value="get" onclick="getObj()"/>
<select id="someElement">
	<option>一班</option>
	<option>二班</option>
	<option>三班</option>
</select>

<script>
function getObj(){
	var $obj = $('#someElement').find('option:selected');
	alert( $obj.val() );
}
</script>
</body>
</html>

  14.获取选中的下拉框

<!DOCTYPE>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../scripts/jquery.js" type="text/javascript"></script>

</head>
<body>
<button  >toggle</button>
<input type="checkbox" value="1" />篮球
<input type="checkbox" value="2" />足球
<input type="checkbox" value="3" />羽毛球

<script>
var tog = false; 
$('button').click(function(){
    $("input[type=checkbox]").attr("checked",!tog);
    tog = !tog;
});
</script>
</body>
</html>

  15.使用siblings()来选择同辈元素

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../scripts/jquery.js" type="text/javascript"></script>
<style>
li.active{
	font-size:20px;
	color:red;
}
</style>
</head>
<body>
<ul id="nav">
	<li>Google</li>
	<li>百 度</li>
	<li>新浪</li>
</ul>
<script>
/* 不这样做
$('#nav li').click(function(){
    $('#nav li').removeClass('active');
    $(this).addClass('active');
});
*/
//替代做法是
$('#nav li').click(function(){
    $(this).addClass('active')
           .siblings().removeClass('active');
});

</script>
</body>
</html>

  16.从元素中除去HTML

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../scripts/jquery.js" type="text/javascript"></script>
</head>
<body>
<div>
<p>锋利的<a href="#nogo">jQuery</a></p>
</div>
<script>
(function($) { 
$.fn.stripHtml = function() { 
  var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi; 
  this.each(function() { 
    $(this).html( $(this).html().replace(regexp,'') ); 
  });
  return $(this); 
} 
})(jQuery); 
//用法: 
$('div').stripHtml(); 
</script>
</body>
</html>

  这些只是一部分,全部的代码(上)面有链接。

原文地址:https://www.cnblogs.com/akou/p/4477706.html