点击事件:
js中所有的事件都是以on开头,去掉on就是jQuery中的事件名,
js中所有的功能都是以属性为主,jQuery中是函数为主。
<body>
<button>按钮1</button>
<div>
今日头条:中国
</div>
<script src="./jquery/jquery-3.1.1.js"></script>
<script type="text/javascript">
let $btn = $('button');
$btn.click(function(event){
console.log($(this).text());
console.log($('div').html());
console.log(event.target);
});
</script>
</body>
案例:
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
ul{
padding: 0;
list-style: none;
}
ul li{
cursor: pointer;
}
</style>
</head>
<body>
<ul>
<li class="title">
<h4>电子产品</h4>
<ul style="display: none;" onclick="stop(event)">
<li>手机</li>
<li>手表</li>
</ul>
</li >
<li class="title">
<h4>衣服</h4>
<ul style="display: none;">
<li>阿迪</li>
<li>李宁</li>
</ul>
</li>
</ul>
<script src="./jquery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$('.title').click(function(){
let ulNode = this.getElementsByTagName('ul')[0];
let $ulNode = $(ulNode);
if($ulNode.css('display') == "none"){
$ulNode.css('display','block')
}else{
$ulNode.css('display','none')
}
})
function stop(e){
e.stopPropagation();
}
</script>
</body>
焦点事件/失去焦点事件
<style type="text/css">
input{
background-color: white;
}
</style>
<body>
<input type="text">
<script src="./jquery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$('input').on({
focus:function(){
$(this).css("backgroundColor","darkorange");
},
blur:function(){
$(this).css("backgroundColor","white");
}
})
</script>
</body>
onchange事件
<body>
<input type="text">
<select name="">
<option value="">123</option>
<option value="">456</option>
<option value="">789</option>
</select>
<script src="./jquery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$('input').change(function(){
console.log($(this).val());
})
$('select').change(function(){
console.log(this.selectedIndex);
})
</script>
</body>