jQuery对象和dom对象的转化

jquery对象 和 dom对象 的转化


jquery对象转化为dom对象: $()[下标]

<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        function f1(){
            //jquery对象 转化为 dom对象 进而调用dom的成员
            $('#apple')[0].style.backgroundColor = "blue";
            $('li')[1].style.backgroundColor = "brown";
        }
        </script>
        <style type="text/css">
        div {width:300px;height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <div id="apple"></div>
        <ul>
            <li>北京</li>
            <li>上海</li>
            <li>深圳</li>
        </ul>
        <input type="button" value="触发1" onclick="f1()" />
    </body>
</html>


dom对象转化为jquery对象:$(dom对象)

<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        function f1(){
            //dom对象 变为 jquery对象: $(dom对象)
            //注意:每次只能是"一个"dom对象变为jquery对象
            var dv = document.getElementById('apple');
            $(dv).css('background-color','green');

            var lis = document.getElementsByTagName('li');
            $(lis[2]).css('color','red');
        }
        </script>
        <style type="text/css">
        div {width:300px;height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <div id="apple"></div>
        <ul>
            <li>北京</li>
            <li>上海</li>
            <li>深圳</li>
        </ul>
        <input type="button" value="触发1" onclick="f1()" />
    </body>
</html>


Weiter lernen
||
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> function f1(){ //jquery对象 转化为 dom对象 进而调用dom的成员 $('#apple')[0].style.backgroundColor = "blue"; $('li')[1].style.backgroundColor = "brown"; } </script> <style type="text/css"> div {width:300px;height:200px; background-color:pink;} </style> </head> <body> <div id="apple"></div> <ul> <li>北京</li> <li>上海</li> <li>深圳</li> </ul> <input type="button" value="触发1" onclick="f1()" /> </body> </html>
einreichenCode zurücksetzen
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!