溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jQuery基礎(chǔ)語法有哪些

發(fā)布時間:2022-02-22 09:53:37 來源:億速云 閱讀:121 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下jQuery基礎(chǔ)語法有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

jQuery 語法
通過 jQuery,您可以選?。ú樵?,query) HTML 元素,并對它們執(zhí)行“操作”(actions)。
jQuery 語法是為 HTML 元素的選取編制的,可以對元素執(zhí)行某些操作。基礎(chǔ)語法如下:

$(selector).action()

解釋:
    美元符號 $ 定義 jQuery
    選擇符(selector)“查詢”和“查找” HTML 元素
    jQuery 的 action() 執(zhí)行對元素的操作
示例:
    $(this).hide() - 隱藏當(dāng)前元素
    $("p").hide() - 隱藏所有段落
    $(".test").hide() - 隱藏所有 class="test" 的所有元素
    $("#test").hide() - 隱藏所有 id="test" 的元素
提示:jQuery 使用的語法是 XPath 與 CSS 選擇器語法的組合。

實例:$(this).hide()

<html>
<head>
<script type="text/javascript" src="../jquery/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $(this).hide();
        });
    });
</script>
</head>
<body>
    <button type="button">Click me</button>
</body>
</html>

注釋:本實例演示了 jQuery hide() 函數(shù),隱藏當(dāng)前的 HTML 元素。

實例:$("#test").hide()

<html>
<head>
<script type="text/javascript" src="../jquery/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("#test").hide();
        });
    });
</script>
</head>
<body>
    <h3>This is a heading</h3>
    <p>This is a paragraph.</p>
    <p id="test">This is another paragraph.</p>
    <button type="button">Click me</button>
</body>
</html>

注釋:本實例演示了 jQuery hide() 函數(shù),隱藏 id="test" 的元素。

實例:$("p").hide()

<html>
<head>
<script type="text/javascript" src="../jquery/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("p").hide();
        });
    });
</script>
</head>
<body>
    <h3>This is a heading</h3>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <button type="button">Click me</button>
</body>
</html>

注釋:本實例演示了 jQuery hide() 函數(shù),隱藏所有 <p> 元素。

實例:$(".test").hide()

<html>
<head>
<script type="text/javascript" src="../jquery/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $(".test").hide();
        });
    });
</script>
</head>
<body>
    <h3 class="test">This is a heading</h3>
    <p class="test">This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <button type="button">Click me</button>
</body>
</html>

注釋:本實例演示了 jQuery hide() 函數(shù),隱藏所有 class="test" 的元素。

文檔就緒函數(shù)
您也許已經(jīng)注意到在我們的實例中的所有 jQuery 函數(shù)位于一個 document ready 函數(shù)中:

$(document).ready(function(){
    // jQuery functions go here
});

這是為了防止文檔在完全加載(就緒)之前運行 jQuery 代碼。
如果在文檔沒有完全加載之前就運行函數(shù),jQuery 操作可能失敗。

看完了這篇文章,相信你對“jQuery基礎(chǔ)語法有哪些”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI