会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132468个问题

老师,对于document.body.clientWidth/document.body.clientHeight

document.body.offsetWidth/document.body.offsetHeight

document.body.scrollWidth/document.body.scrollHeight

document.body.scrollTop/document.body.scrollLeft

这几个有点不太明白,我理解是offsetWidth=clientWidth+border;可是在变网页窗口的时候,offsetWidth比clientWidth小。不知道为什么我这么写之后,网页就不显示div了。

f5b1aa9a5fa1d7bc4d2e63a7ca19485.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    </style>
<body style="height: 1000px">
<div style="background: #0d3349;width: 300px;height: 2000px"></div>
</body>
    <script type="text/javascript">
        window.onload=function(){
            document.write("网页可见区域宽:"+document.body.clientWidth+"</br>");
            document.write("网页可见区域高client:"+document.body.clientHeight+"</br>");
            document.write("网页可见区域宽:"+document.body.offsetWidth+"</br>");
            document.write("网页可见区域高offset:"+document.body.offsetHeight+"</br>");
            document.write("网页正文全文宽:"+document.body.scrollWidth);
            document.write("网页正文全文高:"+document.body.scrollHeight);
            document.write("网页被卷去的高:"+document.body.scrollTop);
            document.write("网页被卷去的左:"+document.body.scrollLeft);
            document.write("网页正文部分上:"+window.screenTop);
            document.write("网页正文部分左:"+window.screenLeft);
            document.write("网页正文部分上:"+window.screenX);
            document.write("网页正文部分左:"+window.screenY);
            document.write("屏幕分辨率的高:"+window.screen.height);
            document.write("屏幕分辨率的宽:"+window.screen.width);
            document.write("屏幕可用工作区高度:"+window.screen.availHeight);
            document.write("屏幕可用工作区宽度:"+window.screen.availWidth);
            document.write("浏览器高度:"+window.outerHeight+"</br>");
            document.write("浏览器宽度:"+window.outerWidth+"</br>");
            document.write("浏览器内页面可用高度:"+window.innerHeight+"</br>");
            document.write("浏览器内页面可用宽度:"+window.innerWidth);
        }
    </script>
</head>
</html>


WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 30871楼
JAVA 全系列/第六阶段:项目管理与SSM框架/RBAC实战 30874楼

问题一:

老师,我想在点击新增一行这个按钮时新增的一行可以自定义书名,作者,数量,但是我发现我写的代码只能自定义书名和作者,而且数量随着书名和作者的值变化而变化,如果先在书名里面写张三那么数量也变为张三,然后在作者里面写李四,这时候数量又变成了李四,老师这个我该怎么解决


问题二:

我在书名,作者和数量的input里面都调用了失去焦点的方法,为什么只有数量可以成功调用,写完之后鼠标点击其他地方文本框边框消失,而作者和书名却不行


问题动图如下:


GIF.gif



源码如下:


html:

<html>
	<head>
		<title>jQuery操作表格</title>
		<meta charset="UTF-8"/>
		<!--声明css代码域-->
		<style type="text/css">
			tr{
				height: 40px;
			}
		</style>
	 
	 	<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script>
		
		<script src="js/gwc.js"	type="text/javascript" charset="UTF-8"></script>
		
	</head>
	<body>
		<h3>jQuery操作表格</h3>
		<hr />
		<input type="button" id="fx" value="反选" />
		<input type="button" id="addRow" value="新增一行" />
		<input type="button" id="delRow" value="删除行" />
		<input type="button" id="copyRow" value="复制行" />
		<table border="1px" cellpadding="10px" cellspacing="0" id="ta">
			<tr>
				<td width="50px"><input type="checkbox" name="chks" id="chks" value="1" /></td>
				<td width="200px">书名</td>
				<td width="200px">作者</td>
				<td width="200px">数量</td>
				<td width="200px">操作</td>
			</tr>
			<tr id="">
				<td><input type="checkbox" name="chk" id="" value="2"/></td>
				<td>《Java编程之道》</td>
				<td>wollo</td>
				<td>10</td>
				<td>
					<input type="button" name="aa" id="" value="修改数量"  onclick="change(this)"/>
					<input type="button" name="" id="" value="删除" onclick="del(this)"/>
				</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="chk" id="" value="3" /></td>
				<td>《Python和我的故事》</td>
				<td>赵老师</td>
				<td>10</td>
				<td>
					<input type="button" name="" id="" value="修改数量" onclick="change(this)"/>
					<input type="button" name="" id="" value="删除" onclick="del(this)"/>
				</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="chk" id="" value="4" /></td>
				<td>《web开发详解》</td>
				<td>张老师</td>
				<td>30</td>
				<td>
					<input type="button" name="" id="" value="修改数量" onclick="change(this)"/>
					<input type="button" name="" id="" value="删除" onclick="del(this)"/>
				</td>
			</tr>			
		</table>
	</body>
</html>


JS:

$(function(){
	
	//确定全选和全部不选操作
	
	$("#chks").click(function(){
		
		var flag = $(this).prop("checked");//返回布尔类型
		
			$("input[name=chk]").prop("checked",flag);//让所有内容都选中

	})
	
	//判断是否全选的操作
	$("input[name=chk]").click(function(){
		
		var flag = true;
		
		var chk = $("input[name=chk]");
		
		//each:遍历		每次遍历执行funaction 里面的内容
		chk.each(function(){
			
			if(!$(this).prop("checked")){
				
				flag = false;
				
				return;
			}
			
		})
		
		$("#chks").prop("checked",flag);
		
	})
	
	//反选的操作
	$("#fx").click(function(){
		
		//拿到所有的多选框
		var chx = $("input[name=chk]");
		
		//遍历
		chx.each(function(){
			
			//获得每个多选框的初始状态
			var flag = $(this).prop("checked");
			
			//现有状态与原始状态进行取反
			$(this).prop("checked",!flag);
		})
	})
	
	//新增加一行的操作
	$("#addRow").click(function(){
		
		//获得tab对象
		var tab = $("#ta");
		
		tab.append('<tr id="">'+
				'<td><input type="checkbox" name="chk" id="" value="2"/></td>'+
				'<td><input type="text"	onblur="bul(this)"/></td>'+
				'<td><input type="text"	onblur="bul(this)"/></td>'+
				'<td><input type="text"	onblur="bul(this)"/></td>'+
				'<td>'+
					'<input type="button" name="aa" id="" value="修改数量"  onclick="change(this)"/>&nbsp'+
					'<input type="button" name="" id="" value="删除" onclick="del(this)"/>'+
				'</td>'+
			'</tr>');
		
	})
	
	//删除的操作
	$("#delRow").click(function(){
		
		//获得被选中的name=chk的单选框
		var del = $("input[name=chk]:checked");
		
		if(del.length==0){//如果一行没选
			alert("至少选择一行!");
		}else{
			
			//执行删除整行的操作	单选框的父节点是	td	,	td	的父节点是	tr
			del.parent().parent().remove();
		}
		
	})
	
	//复制行		选中几个复制几个
	$("#copyRow").click(function(){
		
		//获得被选中的name=chk的多选框
		var copy = $("input[name=chk]:checked");
		
		if(copy.length==0){//如果一行没选
			alert("至少选择一行!");
		}else{
			//执行copy的操作
			
			//复制
			var tr = copy.parent().parent().clone();//克隆整行
			//黏贴
			$("#ta").append(tr);
			
		}
	})
	
})

//修改数量的操作

function change(th){//这里的this是JS类型的,得转换成JQ对象
	
	//$(th):转换成JQ对象		获得	tr	结点
	var par = $(th).parent().parent();
	
	//将写死的数量变成可以定义的文本框
	par.children().eq(3).html("<input	type='text' size='3px' onblur='bul(this)'/>");
}

function bul(th){
	
	var par = $(th).parent().parent();
	
	par.children().eq(3).html(th.value);//或者转成JQ对象再调用val来获得值
}

function del(th){
	
	var par = $(th).parent().parent();
	
	par.remove();
	
}


JAVA 全系列/第四阶段:网页编程和设计/Jquery(旧) 30875楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 30876楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 30878楼
Python 全系列/第十二阶段:Python_Django3框架/Django高级 30883楼
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 30884楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637