简体   繁体   中英

Setting Values to Non-Existent Elements

Whether via JavaScript or jQuery, are there any detriments to setting values to elements that don't exist?

In some of my generic functions that addresses dynamically-built DOMS, some class elements are assigned values and attributes but they might not always exist.

You can create elements that aren't connected to the DOM and do any operations to them that you'd do to normal DOM element. This is often a better approach because all your changes to the disconnected DOM element won't cause the browser to redraw. Then, after you've applied all your changes, you can attach that element to the DOM and only cause the browser to redraw once.

Likewise, you can assign as many classes as you wish, whether there are styles associated with them or not doesn't matter.

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