首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在javascript中用另一个div替换div?

如何在javascript中用另一个div替换div?
EN

Stack Overflow用户
提问于 2016-05-20 21:14:40
回答 6查看 43.7K关注 0票数 4

如何在javascript中用另一个div替换div?

这就是我所拥有的:

代码语言:javascript
复制
<div id="main_place">
main
</div>

<button onclick="show(operation1)">Replace to operation 1</button>
<button onclick="show(operation2)">Replace to operation 2</button>
<button onclick="show(operation3)">Replace to operation 3</button>


<div id=operation1 style=“display:none”>
Some textboxes and text
</div>

<div id=operation2 style=“display:none”>
Again some textboxes and text
</div>

<div id=operation3 style=“display:none”>
And again some textboxes and text
</div>

<script>
function show(param_div_id){
        document.getElementById('main_place').innerHTML = Here should be div from param;
        }
</script>

有没有可能在没有jquery的情况下完成呢?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-05-20 21:22:21

将字符串传入您的方法并获取另一个div

代码语言:javascript
复制
<div id="main_place">
  main
</div>

<button onclick="show('operation1')">Replace to operation 1</button>
<button onclick="show('operation2')">Replace to operation 2</button>
<button onclick="show('operation3')">Replace to operation 3</button>


<div id=operation1 style=“display:none”>
  Some textboxes and text
</div>

<div id=operation2 style=“display:none”>
  Again some textboxes and text
</div>

<div id=operation3 style=“display:none”>
  And again some textboxes and text
</div>

<script>
  function show(param_div_id) {
    document.getElementById('main_place').innerHTML = document.getElementById(param_div_id).innerHTML;
  }
</script>

票数 16
EN

Stack Overflow用户

发布于 2019-03-06 00:05:30

我用这个表单替换了我的div。

代码语言:javascript
复制
<div id="newDiv" class="here" style="display: none" > content</div>
<div id="oldDiv" class="here" style="display: block"> content</div>
<button type="button" onclick="newDivHere()">replace</button>

和JS:

代码语言:javascript
复制
function newDivHere() {
document.getElementById("oldDiv").style.display="none";
document.getElementById("newDiv").style.display="block";
}
票数 1
EN

Stack Overflow用户

发布于 2021-11-28 22:00:56

试着这样做

document.querySelector('#maincontent').outerHTML = jsonResponse.main;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37347690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档