简体   繁体   中英

How can I tell of a HTML attribute exists or not, using Javascript

I'm trying to convert to HTML5, but to preserve the site experience for those that don't have all the HTML5 features, I would use a workaround. For example, how can I check if the HTML5 attribute contentEditable exists, so that if it doesn't, I can just create a textarea instead.

Thaks!

这可以提供帮助: http//diveintohtml5.ep.io/detect.html

The following snippet checks if the 'contentEditable' property exists.

if('contentEditable' in element){
   // contentEditable is available
}
else{
   // create textarea
}

According to HTML5 Peeks, Pokes and Pointers :

"isContentEditable" in document.createElement("a")

You can replace the document.createElement("a") with any element, really, so if you already have an element you're working with, use that.

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