簡體   English   中英

如何檢查瀏覽器對HTML5表單屬性的支持?

[英]How to check browser support for HTML5 form attributes?

如何檢查瀏覽器是否支持formtargetform屬性?

Modernizr在這里無濟於事,我什至無法在瀏覽器上的form支持的當前狀態下在網上找到任何東西。 盡管進行了快速測試,但除IE 9之外的所有當前瀏覽器均支持它們。

那么,如何檢查瀏覽器是否處理form*屬性?

干得好:

if("formtarget" in document.createElement("input")){
    //good job, browser
}else{
    //nope
}

輸入form屬性是一種特殊情況。 它在HTML5功能之前用於引用父表單,但是現在它也用作屬性,因此您將對IE產生誤報。

有一個檢查功能,但它涉及與DOM的交互,這可能會影響性能,但是無論如何您還是要這樣做。 希望能幫助到你。

function testInputFormSupport() {
    var input = document.createElement('input'),
         form = document.createElement('form'),
         formId = 'test-input-form-reference-support';
    // Id of the remote form
    form.id = formId;
    // Add form and input to DOM
    document.body.appendChild(form);
    document.body.appendChild(input);
    // Add reference of form to input
    input.setAttribute('form', formId);
    // Check if reference exists
    var res = !(input.form == null);
    // Remove elements
    document.body.removeChild(form);
    document.body.removeChild(input);
    return res;
}

看看這個網站 它涵蓋了所有新的表單功能,並提供了一個方便的小功能來測試屬性的支持

// Function to test for attribute support
function elSupportsAttr(el, attr) {
  return attr in document.createElement(el);
}

嘗試

if ("attribute" in document.createElement("tag"))

如何使用Javascript判斷HTML屬性是否存在

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM