Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript获取DIV的内容(Get content of a DIV using JavaScript)

I have two DIV's called DIV1 and DIV2 and DIV1 consists of dynamic content and DIV2 is empty.

(我有两个名为DIV1和DIV2的DIV,DIV1由动态内容组成,DIV2为空。)

I need content of DIV1 to be displayed in DIV2.

(我需要在DIV2中显示DIV1的内容。)

How can I do it.

(我该怎么做。)

I coded in below manner which is not working.

(我以下面的方式编码,但是没有用。)

Anybody please correct it.

(任何人请纠正它。)

<script type="text/javascript">

   var MyDiv1 = Document.getElementById('DIV1');
   var MyDiv2 = Document.getElementById('Div2');
   MyDiv2.innerHTML = MyDiv2; 

</script>
<body>
<div id="DIV1">
 // Some content goes here.
</div>

<div id="DIV2">
</div>
</body>
  ask by lock translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(1) Your <script> tag should be placed before the closing </body> tag.

((1)您的<script>标签应放在结束</body>标签之前。)

Your JavaScript is trying to manipulate HTML elements that haven't been loaded into the DOM yet.

(您的JavaScript正在尝试操作尚未加载到DOM中的HTML元素。)


(2) Your assignment of HTML content looks jumbled.

((2)您对HTML内容的分配看起来很混乱。)


(3) Be consistent with the case in your element ID, ie 'DIV2' vs 'Div2'

((3)与元素ID中的情况一致,即'DIV2'与'Div2')


(4) User lower case for 'document' object (credit: ThatOtherPerson)

((4)“文档”对象的用户小写(信用:ThatOtherPerson))

<body>
<div id="DIV1">
 // Some content goes here.
</div>

<div id="DIV2">
</div>
<script type="text/javascript">

   var MyDiv1 = document.getElementById('DIV1');
   var MyDiv2 = document.getElementById('DIV2');
   MyDiv2.innerHTML = MyDiv1.innerHTML;

</script>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...