简体   繁体   中英

Create a DOM element with jQuery

I created a input element with jQuery like that:

var element = $('<input />', {
  name: name1,
  id: id1,
  type: 'text'
});

And I want to return the DOM element associated with it. However, element is not a DOM (yet). How can I transform it to DOM element?

您可以使用element[0]element.get(0)

jQuery returns an array-like object of DOM elements. Using the array indexer will always allow you to get a specific element, like so:

var domElement = element[0];

Alternatively, jQuery provides a function just for that, .get :

var domElement = element.get(0);

you can check this solutions to use maybe a DOM parser. Converting HTML string into DOM elements?

or just use some native javascript functions. Creating a new DOM element from an HTML string using built-in DOM methods or prototype

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM