當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


underscore.js _.isElement()用法及代碼示例

_.isElement() 函數:用於檢查元素是否為文檔對象模型。文檔對象模型是 javascript 查看包含頁麵數據的方式。級聯樣式表 (CSS) 和 javascript (JS) 與文檔對象模型 (DOM) 交互。

用法:

_.isElement(object)

參數:
它隻需要一個參數,即需要檢查的對象元素。

返回值:
如果它是 DOM 元素,則返回 true,否則返回 false。



例子:

  1. 將 html 標記傳遞給 _.isElement() 函數:
    _.isElement() 函數獲取元素並執行檢查函數。它檢查它是否是 DOM 元素。像這裏一樣,傳遞給 _.isElement() 函數的參數是 ‘html’ ,因為我們知道它是一個 DOM 元素,所以在輸出中返回 true。
    
    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src = 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
         </script>
      
        <script src=
        "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isElement(jQuery('html')[0]));
        </script>
    </body>
       
    </html>

    輸出:

  2. 將 body 標簽傳遞給 _.isElement() 函數:
    在這種情況下,我們將 ‘body’ 標記作為參數傳遞給 _.isElement() 函數。因為,我們知道 ‘body’ 標簽是一個 DOM 元素,所以輸出為真。
    
    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src = 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
        </script>
      
        <script src=
        "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isElement(jQuery('body')[0]));
        </script>
    </body>
       
    </html>

    輸出:

  3. 將 div 標簽傳遞給 _.isElement() 函數:
    在這種情況下,我們將 ‘div’ 標記作為參數傳遞給 _.isElement() 函數。因為,我們知道 ‘div’ 標簽是一個 DOM 元素,所以輸出為真。
    
    <html>
       
    <head>
        <script src = 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
        </script>
        <script src=
        "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isElement(_.isElement(jQuery('div')[0])));
        </script>
    </body>
       
    </html>

    輸出:

  4. 在 _.isElement() 函數中使用 and (&&) 操作:
    我們甚至可以使用 2 個 _.isElement() 函數來獲得輸出,如下例所示。首先,將計算他們的兩個答案,然後執行 ‘and’ 操作。隻有當兩個答案都為真時,AND 運算才會給出真,否則它將給出假作為答案。
    
    <!-- Write HTML code here -->
      
    <html>
       
    <head>
        <script src = 
        "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
         </script>
      
        <script src=
        "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
         console.log(_.isElement(jQuery('html')[0]) && _.isElement(jQuery('div')[0]));
         </script>
    </body>
       
    </html>

    輸出:

    `

筆記:
這些命令在 Google 控製台或 Firefox 中不起作用,因為需要添加他們沒有添加的這些附加文件。
因此,將給定的鏈接添加到您的 HTML 文件中,然後運行它們。
鏈接如下:


<!-- Write HTML code here -->
<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
  
<?-- For jquery to work include the below script --?>
<script src=
"https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
</script>

一個例子如下所示:




相關用法


注:本文由純淨天空篩選整理自Sakshi98大神的英文原創作品 Underscore.js _.isElement() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。