2017-10-09 63 views
-5

嗨我想添加特定的HTML行到特定的地方与JavaScript代码。使用javascript添加html

<ion-header> 
    <ion-navbar> 
    <ion-title>Map</ion-title> 
    </ion-navbar> 
    <script src='https://api.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.js'></script> 
    <link href='https://api.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.css' rel='stylesheet' /> 
</ion-header> 

<ion-content padding> 

     <!--<div id="map" style="width: 100%; height: 100%"></div>--> <!--Add this line to here with js--> 

    </ion-content> 

我试过document.getelementbyid.inner/outerhtmldocument.write但是搞不清楚。

+2

帖子你尝试过什么,即使它没有工作。 –

+3

javascript是区分大小写的,所以很可能'document.getElementById('elementid')。innerHTML' – Icepickle

+1

如果你真的使用了'document.getelementbyid',你会收到控制台错误。检查[** getElementById **](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById)的文档以及类似的文章,并发布不适用于[* *最小,完整和可验证的示例**](https://stackoverflow.com/help/mcve) – Nope

回答

1

我没有看到任何id或类。因此,而不是尝试使用标签名称

document.getElementsByTagName("ion-content")[0].innerHTML = '<div id="map" style="width: 100%; height: 100%"></div>'; 
+0

@rektandlove我在我的系统中试过我的代码,它工作得很完美。 你可以让我知道你在执行我的代码时得到了什么错误? 检查我编辑的代码 –

+0

谢谢你的代码正在工作我猜,但也许即时通讯尝试以错误的方式解决我的问题。你检查此主题:https://forum.ionicframework.com/t/leaflet-switching-tabs- map-container-is-already-initialized/108124即时尝试删除地图div当我离开地图水龙头并初始化它时,我加载我的地​​图选项卡again.but似乎不工作。 – rektandlove

+0

如果我的代码工作,这意味着它是你的问题的解决方案,为什么给--ve点againts它。 无论如何,在您的代码“”中,如果您严格要使用 “document.getElementbyid” –

0

试试这个:

document.querySelector("ion-content").innerHTML = "<a style='color: red'> ASD</a>" ;
<ion-header> 
 
    <ion-navbar> 
 
    <ion-title>Map</ion-title> 
 
    </ion-navbar> 
 
    <script src='https://api.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.js'></script> 
 
    <link href='https://api.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.css' rel='stylesheet' /> 
 
</ion-header> 
 

 
<ion-content padding> 
 

 
     <!--<div id="map" style="width: 100%; height: 100%"></div>--> <!--Add this line to here with js--> 
 

 
    </ion-content>