简体   繁体   中英

Chrome giving me an error on the JS console: “Uncaught InvalidCharacterError: The string contains invalid characters.”

It is pointing me to line

var T = document.createElement("table id=""+tableID+""");

in which I was trying to create a table element with a particular ID. Any idea what happened? I thought I made a proper escape sequence.

Some IE versions let you have a full tag inside of of document.create, but standards following browsers only accept the tag name. To set the id, you can either set the attribute directly or use setAttribute .

var T = document.createElement("table");
T.id=tableID;

or

var T = document.createElement("table");
T.setAttribute("id", tableID);

The invalid character here is space. document.createElement only takes the element name as the argument, and element names can not contain spaces. So you should create the elemnt first, then set its attributes/properties.

var T = document.createElement('table');
T.id = tableID

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