Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript innerHTML显示方式与预期不同_Javascript_Html_Css_Dom - Fatal编程技术网

Javascript innerHTML显示方式与预期不同

Javascript innerHTML显示方式与预期不同,javascript,html,css,dom,Javascript,Html,Css,Dom,文本 更新 试试看 函数myFunction(){ document.getElementById(“demo”).innerHTML=document.getElementById(1.innerHTML); } 函数测试函数(el){ var attr=document.createAttribute('contenteditable'); attr.value='true'; el.parentNode.firstElementChild.setAttributeNode(属性) e


  • 文本

    更新
试试看

函数myFunction(){ document.getElementById(“demo”).innerHTML=document.getElementById(1.innerHTML); } 函数测试函数(el){ var attr=document.createAttribute('contenteditable'); attr.value='true'; el.parentNode.firstElementChild.setAttributeNode(属性) el.parentNode.firstElementChild.focus()文件 }
制作HTML元素并给它一个id时,id必须在引号中,并且它还必须包含至少1个字符,并且没有空格,因此我建议将
li
元素的id
id=1
更改为
id=“1a”
(或带有字母的内容),和
document.getElementById(“1a”).innerHTML
应更改为该新值(在本例中为“1a”)

创建HTML元素并为其指定id时,id必须在引号中,并且它还必须包含至少1个字符无空格,因此,我建议将
li
元素的id
id=1
更改为
id=“1a”
(或其中包含字母的内容),并且
document.getElementById(“1a”).innerHTML
应更改为该新值(在本例中为“1a”)

,如果您希望该行看起来像一个项目符号列表,然后需要将
innerHTML
的结果放入无序列表中。现在你把它放在一个段落里

考虑将

更改为:

<ul>
  <li id="demo"></li>
</ul>

如果您希望该行看起来像一个项目符号列表,那么需要将
innerHTML
的结果放在无序列表中。现在你把它放在一个段落里

考虑将

更改为:

<ul>
  <li id="demo"></li>
</ul>

好的,长话短说写作id=1或id='1'不会引起任何问题。 它仍在抓取HTML,并且工作良好。 问题在于,当您将某个东西放入p标记时,它的行为开始不同,(BR)break标记没有显示换行符。我把P改成Div,现在看起来很好。 我还更改/删除了CSS类 。输入组{ 位置:相对位置; 显示:-ms flexbox; /*显示:flex; 现在您可以看到换行


  • 文本

更新 试试看 函数myFunction(){ document.getElementById(“demo”).innerHTML=document.getElementById(1.innerHTML); } 函数testFunction(el1){ el=document.getElementById(el1); var attr=document.createAttribute('contenteditable'); attr.value='true'; el.parentNode.firstElementChild.setAttributeNode(属性) el.parentNode.firstElementChild.focus()文件 }
好的,长话短说写作id=1或id='1'不会引起任何问题。 它仍在抓取HTML,并且工作良好。 问题是当你在p标签中放置一些东西时,它开始表现出不同的行为,(BR)break标签没有显示换行符。我将p改为Div,现在看起来很好。 我还更改/删除了CSS类 。输入组{ 位置:相对位置; 显示:-ms flexbox; /*显示:flex; 现在您可以看到换行


  • 文本

更新 试试看 函数myFunction(){ document.getElementById(“demo”).innerHTML=document.getElementById(1.innerHTML); } 函数testFunction(el1){ el=document.getElementById(el1); var attr=document.createAttribute('contenteditable'); attr.value='true'; el.parentNode.firstElementChild.setAttributeNode(属性) el.parentNode.firstElementChild.focus()文件 }
document.getElementById(1)
不是您想要的。HTML元素
id
值是字符串。请使用
document.getElementById('1')
,但您应该始终使用以字母而不是数字开头的元素
id
值。此外,您应该避免使用
.innerHTML
,因为这是一个昂贵的操作(因为这意味着文本必须由浏览器进行解析和处理)。使用
parent.appendChild(other.cloneNode())
要快得多(而且更简单,依我看),请看这是否回答了您的问题?
document.getElementById(1)
不是您想要的。HTML元素
id
值是字符串。请使用
document.getElementById('1')
,但您应该始终使用以字母而不是数字开头的元素
id
值。此外,您应该避免使用
.innerHTML
,因为这是一个昂贵的操作(因为这意味着文本必须由浏览器进行解析和处理)。使用
parent.appendChild(other.cloneNode())
要快得多(而且更简单,依我看),请看这是否回答了您的问题?