简体   繁体   English

Javascript Object.innerHtml不能替代提供的整个字符串参数

[英]Javascript Object.innerHtml is not substituting the whole string parameter provided

newcell.innerHTML='<td id=\\"blah1\\">'+BLAH+'</td>'; For some strange reason my <td>BLAH</td> is correctly rendered on the page but any attributes that I give inside the td tag just don't display. 由于某些奇怪的原因,我的<td>BLAH</td>可以正确显示在页面上,但是我在td标记内提供的任何属性都不会显示。 Any pointers? 有指针吗? Sorry if this might sound very silly, I have very less experience in front end. 抱歉,这听起来可能很愚蠢,我在前端的经验很少。 Any 'must look' tutorials would be appreciated. 任何“必看”教程都将不胜感激。 Thanks 谢谢

If newCell in this case represents a <td> then you need to either use outerHTML instead of innerHTML , or programmatically set the properties you need. 如果在这种情况下newCell表示<td>则您需要使用outerHTML而不是innerHTML ,或者以编程方式设置所需的属性。

var newCell = document.createElement("td");
newCell.outerHTML = '<td id=\"blah1\">'+BLAH+'</td>';

or 要么

var newCell = document.createElement("td");
newCell.id = "blah1";
newCell.innerHTML = BLAH;

If you're declaring a Javascript string with single quotes, you don't need to escape any double-quotes that you use inside it. 如果要声明带有单引号的Javascript字符串,则无需转义在其中使用的任何双引号。 Try newcell.innerHTML='<td id="blah1">'+BLAH+'</td>'; 尝试newcell.innerHTML='<td id="blah1">'+BLAH+'</td>'; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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