Blogger Information
Blog 38
fans 1
comment 0
visits 24249
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月20日作业:jQuery对象与DOM对象之间转换
鲨鱼辣椒的博客
Original
632 people have browsed it

jQuery对象与DOM对象之间转换

如下代码所示:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery对象与DOM对象之间的转换</title>
</head>
<body>
<ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
</ul>

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

<script>
    //DOM对象与jQuery对象的相互转换
    //DOM转jQuery: $(); 工厂函数
    //jQuery转DOM: $()[],$().get()

    var ul = document.getElementsByTagName('ul')[0];
    var li3 = ul.getElementsByTagName('li')[2];
    li3.style.color = 'red';

    document.querySelector('ul li:nth-of-type(2)').style.color = 'cyan';

    //1. 先将DOM对象转换为jQuery对象
    $('ul li:nth-of-type(2)').css('color','red');
    // $('ul li:nth-of-type(2)').style.color = 'cyan';

    // 3. jQuery 转换DOM对象
    var lis = $('li');
    console.log(lis);

    //lis: 实际上是一个类数组
    console.log(lis[4] instanceof Object);
    console.log(lis[4] instanceof jQuery);

    //$().get(n)
    console.log(lis.get());

</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post