简体   繁体   English

使用 d3.js 将标签替换为不同的 dom 元素

[英]replace tag with with a different dom element using d3.js

Is there a way we can replace an html tag with a different one using d3.js or just Vanilla JS?, for example to replace <table></table> with <div></div>有没有一种方法可以使用d3.js或仅使用 Vanilla JS 将 html 标签替换为不同的标签?例如,将<table></table>替换为<div></div>

<body>
  <div> 
    <table> ...... </table>
  </div> 
</body>

I have tried my implementation but it has resulted in an error.我已经尝试了我的实现,但它导致了一个错误。 I am not sure where I did wrong.我不确定我哪里做错了。 I need some advices on this我需要一些建议

var customStyle = function(){
  var tbl = d3.selectAll("#" + arcapi.chartId() + ' .crosstab-table-container table');
  tbl.attr('class','loppo');
  
  var mySpan = document.createElement("span");
  tbl.parentNode(mySpan, tbl);
}

I guess this code is not supported as I am doing a third party script customization.我猜这个代码不受支持,因为我正在做第三方脚本定制。

How about find the tags you will replace and its parent node, then remove this tag and append the other you want replace to?如何找到您要替换的标签及其父节点,然后删除此标签和 append 其他您要替换的标签?

 const table = d3.select('#table'); const parent = d3.select(table.node().parentNode); table.remove(); parent.append('div').text('append') //console.log(parent)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> <div> 11 <table id="table"><tr><td>hello</td></tr></table> </div>

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

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