jQuery

donghongbz 2020-02-19

jQuery

jQuery API (哪里不会点哪里)

jQuery 库 CDN加速

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>
</head>
<body>

</body>
</html>
选择器
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>
<a href="" id="test-jquery">点击</a>

<script>
    //选择器就是 CSS 选择器
    $('#test-jquery').click(function () {
        alert('点击');
    })
</script>

</body>
</html>

公式:$(selector).action()

$('p').click();     //标签选择器
$('#id').click();       //Id选择器
$('.class').click();    //类选择器
鼠标事件
//当网页加载完毕之后,响应事件
<script>
    $(function(){

});
</script>

操作DOM

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>

<ul id="test-ul">
    <li class="javascript">javascript</li>
    <li name="python">python</li>
</ul>

<script>
    $('#test-ul li[name = python]').text();
    $('#test-ul').html();
    console.log($('#test-ul li[name = python]').text());
    console.log($('#test-ul').html());
</script>

</body>
</html>
节点文本操作
$('#test-ul li[name = python]').text();    //获得值
 $('#test-ul li[name = python]').text('设置值');   //设置值
 $('#test-ul').html();  //获得值
 $('#test-ul').html('<text>123</text>');    //设置值
结果
元素的显示与隐藏
$('#test-ul li[name = python]').show()
$('#test-ul li[name = python]').hide()

相关推荐

Web全栈笔记 / 0评论 2020-06-15