jQuery DOM对象与jQuery对象转换

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div id="d1">
            我是div1
        </div>
        <div class="d2">
            <p>哈哈</p>
        </div>
        <button type="button" id="b1">我是button1</button>
    </body>
    <script type="text/javascript" src="../images/jquery-3.4.1.min.js"></script>
    
    <script type="text/javascript">
        document.getElementById("b1").onclick = function(){
            /* 获取jQuery对象的对应属性 */
            /*有时jQuery对象名也没有$ */
            var $d1 = $("#d1");
            var shuxing = $("#d1").attr("id");
            alert(shuxing);
            alert($d1 instanceof jQuery);
            var dom1 = $d1.get(0);
            alert(dom1 instanceof jQuery);
            /* DOM对象转为jQuery对象 */
            var d3 = $(dom1);
            alert(d3 instanceof jQuery);
            
        }
    </script>
</html>

猜你喜欢

转载自www.cnblogs.com/qilin20/p/12669400.html
今日推荐