jQuery 操作 练习题

1.添加a的属性标签 划入显示 划出隐藏 滑动时跟随鼠标

代码:
JS原生

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin:0;
padding: 0;
}
ul,li{
list-style: none;
}
.tip{
position:absolute;
height: 30px;
line-height: 30px;
background: yellow;
border: 1px solid #000;

}
</style>
<script src="jquery-1.12.4.min.js"></script>
</head>
<body>
<div id="app">
<a href="" title="item1">item 1</a><br><br>
<a href="" title="item2">item 2</a><br><br>
<a href="" title="item3">item 3</a><br><br>
<a href="" title="item4">item 4</a><br><br>

</div>
<script type="text/javascript">
var alink = document.getElementsByTagName('a');
for(i=0;i<alink.length;i++){
alink[i].onmouseover = function(e){//鼠标划入
e = e||window.event; //兼容火狐和谷歌
var odiv = document.createElement('div');//创建dom元素
odiv.className= "tip";//更改class/id
this.newtitle = this.title;//添加自定义属性
odiv.innerHTML = this.newtitle;
//更改元素文本this指向划入的那个a
this.title = ""//清空这个titile
odiv.style.top = e.clientY+20+'px';//e.clientY鼠标距离屏幕最上面的值
odiv.style.left = e.clientX+20+'px';
document.body.appendChild(odiv);//添加dom元素


}
alink[i].onmouseout = function(){
var odiv1 =document.getElementsByClassName("tip")[0];//找到那个节点
document.body.removeChild(odiv1);//移出那个节点
this.title = this.newtitle;
}
alink[i].onmousemove = function(e){//鼠标移动事件
e = e||window.event;
var odiv2 = document.getElementsByClassName("tip")[0];
odiv2.style.top = e.clientY+20+'px';
odiv2.style.left = e.clientX+20+'px';

}
}
</script>
</body>
</html>
2.jquery:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin:0;
padding: 0;
}
ul,li{
list-style: none;
}
.tip{
position:absolute;
height: 30px;
line-height: 30px;
background: yellow;
border: 1px solid #000;

}
</style>
<script src="jquery-1.12.4.min.js"></script>
</head>
<body>
<div id="app">

<ul id="ul">
<li title="li1">li1</li>
<li title="li2">li2</li>
<li title="li3">li3</li>
<li title="li4">li4</li>
</ul>
<a href="" title="item1" class="xxx">item 1</a><br><br>
<a href="" title="item2" class="xxx">item 2</a><br><br>
<a href="" title="item3" class="xxx">item 3</a><br><br>
<a href="" title="item4" class="xxx">item 4</a><br><br>

</div>
<script type="text/javascript">
$(function(){
$(".xxx").hover(function(e){
e = e||window.event;
this.newtitle = this.title;
$tip = $('<div class="tip">'+this.newtitle+'</div>');
$tip.offset({
top:e.pageY+20,
left:e.pageX+20,
})
$tip.appendTo('body');
this.title = "";
},function(e){
e = e||window.event;
$tip = $(".tip").remove();
this.title = this.newtitle;

})
$(".xxx").on("mousemove",function(e){
e = e||window.event;
var $tip = $(".tip").offset({
top:e.pageY+20,
left:e.pageX+20,
})
})
})
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(答案见下载资源) 上机任务1 用chrome打开dom.sample2.html页面, 在chrome的控制台中输入jQuery代码并执行,完成以下选择动作: (1)查找id号为tigerLily的元素 (2)查找拥有类myList的元素 (3)查找所有的input元素 (4)查找所有img元素和tr元素 (5)查找id号为coffeePot和id号为someDiv的元素 (6)选择具有id属性的所有元素 (7)选择具有id属性的input元素 (8)选择其value属性等于A的元素 (9)选择其value属性等于A或等于C的元素 (10)选择其title属性值中含有dog的img元素 (11)选择其href属性值以http开头的a元素 (12)选择div元素内嵌套的span元素 上机任务2 用chrome打开dom.sample2.html页面, 在chrome的控制台中输入jQuery代码并执行,完成以下选择动作: (13)选择元素li,该元素作为拥有myList类的ul元素的直接子元素 (14)选择id号为radioA的input元素后面的第一个input兄弟元素 (15)选择id号为radioA的input元素后面的所有input兄弟元素 (16)选择dom sample页面中第3个tr元素 (17)选择其type属性值checkbox且索引为偶数的input元素 (18)选择非最后一行的tr元素 (19)选择所有的checkbox元素 (20)选择所有被选中的表单元素 (21)选择含有1972的td元素 (22)选择包含有sapn元素的div元素 (23)选择表格中每行的第一个单元格和最后一个单元格 (24)选择表格中第3行,标题行不算(要求用nth-child()过滤器) 上机任务3 用chrome打开dom.sample2.html页面,该页面中内嵌有几个CSS类:.red .green .blue .yellow .thickBorder .seeThrough 在chrome的控制台中输入jQuery代码并执行,完成以下选择动作: (1)将拥有alt属性的倒数第二个图片元素应用yellow类 (2)将第一个checkbox表单元素应用.green样式 (3)求id值为checkbox3的元素在所有表单元素里的顺序号 (4)用一条链式语句完成:先对所有img元素应用seeThrough样式,再为所有img元素和tr元素应用yellow样式 (5)用filter方法实现:选择单元格内容为Java或Smalltalk的单元格 (6)取图片集中的第2,3,4张图片 (7)用has方法实现:选择那些包含有ul元素的li元素 (8)将tbody每个单元格里的文字设为:我爱jQuery (9)查找form元素的后代元素中标签为label的元素 (10)查找表单元素中是否有id值为checkbox5的元素 提示: 为某dom元素应用某css样式可以使用jQuery中的addClass方法,例如:将id为tt的div元素应用.bt样式,可用些语句:$("#tt").addClass("color","red");

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值