8.7.1、jQuery遍历
return false 阻止a标签的跳转
load()加载服务器上的数据
<div class="hand">
<a href="html/html.html">html</a>
<a href="html/css.html">html</a>
</div>
<div id="app">
</div>
<div class="footer">
footer
</div>
<script>
var htmls = ["html/html.html","html/css.html"];
$("app").load(htmls[0])
$(".head a").click(function(){
$("#app").load(htmls[$(this).index()])
return false;
})
</script>
8.7.2、val
$(element).val() —获取value值
$(element).val(value) —改变value值
<input type="text" value="你">
<script>
var value = $("input").val("")
console.log(value)
</script>
8.7.3、jquery-js/ js-jquery
jquery对象转为js之后,才能使用原生的方法
jquery-js
<div>
div
</div>
<script>
var div = $("div")[0];
var content = div.innerHTML;
console.log(content);
</script>
js-jquery
html();
innerHTML
原生的JS对象转换为jQuery对象 $(element)
<div>
div
</div>
<script>
var div = document.getElementsByTagName("div")[0];
console.log($(div).html());
</script>
8.7.4、jquery方法
<ul>
<li>html</li>
<li>css</li>
<li>javascript</li>
</ul>
<script>
var arr = $("li").filter(item=>{
console.log(item)
})
</script>
8.7.5、find 获取父元素下的子元素
<div id="app">
<div class="item">hello world</div>
<div>div</div>
</div>
<script>
var item = $("#app").find(".item");
var i = $("#app div").filter(".item");
console.log(item)
console.log(i)
</script>
获得父元素
children()
siblings()
parent()
获取所有的兄弟元素
$("li").click(function(){
$(this).css({color:"red"}).siblings().css({color:"green"})
})
console.log($("li").parent()) //获取父元素
8.7.6、判断滚动条是否达到底部
<script>
$(window).scroll(function () {
console.log(isReachBottom())
if(isReachBottom()){
/*发送http请求 */
}
})
function isReachBottom() {
var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的高
var availHeight = $(window).height(); //获取可视区域的高
var dw = $(document).height(); //获取内容区域的高
return (scrollTop + availHeight) > dw -200 ? true : false;
}
</script>
8.7.7、点击标签滚动至内容区域(例子)
*{
padding: 0;
margin: 0;
}
.body{
height:3000px;
}
.head{
left: 0;
top: 0;
height:50px;
position: fixed;
}
div{
width:100px;
height:100px;
background-color: red;
}
#html{
margin-top: 500px;
}
#css{
margin-top: 500px;
}
#js{
margin-top: 500px;
}
</style>
</head>
<body>
<p class="head">
<a href="#html">html</a>
<a href="#css">css</a>
<a href="#js">javascript</a>
</p>
<div id="html">
html
</div>
<div id="css">
css
</div>
<div id="js">
javascript
</div>
<script>
$(".head a").click(function(){
var id = $(this).attr("href");
var offsetTop = $(id).offset().top;
console.log(offsetTop)
$("html,body").animate({scrollTop:offsetTop})
return false;
})
</script>