我试图使用普通的java脚本在变量中提取内容。 我试图只获取带有id的变量的一部分。 在这个例子中,我试图只用id'容器'来拉取部分html。
我的小提琴 https://jsfiddle.net/a2ngc9jv/2/
var htmlPage = `
<html>
<div>
testing but not required
</div>
<div id="container">
<p>
i want to pull this whole block from parent
</p>
<p>
test 2
</p>
</div>
</html>`;
console.log(htmlPage.getElementById("container"))
console.log(document.getElementById("container"))
答案 0 :(得分:1)
htmlPage是一个字符串而不是文档节点。但是很容易把它合二为一:
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { HttpModule, JsonpModule } from '@angular/http';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { EstadosPage } from '../pages/estados/estados';
import { OutrosPage } from '../pages/estados/outros';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
@NgModule({
declarations: [
MyApp,
HomePage,
ListPage,
EstadosPage,
OutrosPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
HttpModule,
JsonpModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
ListPage,
OutrosPage,
EstadosPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
&#13;
答案 1 :(得分:1)
如果您在浏览器中,可以使用DOMparser()
将其转换为真正的dom对象,然后您可以调用要从中获取元素的方法。
var htmlPage = `
<html>
<div>
testing but not required
</div>
<div id="container">
<p>
i want to pull this whole block from parent
</p>
<p>
test 2
</p>
</div>
</html>`;
var parser=new DOMParser();
var htmlDoc=parser.parseFromString(htmlPage, "text/html");
console.log(htmlDoc.getElementById("container"))
console.log(htmlDoc.getElementById("container").innerHTML)
&#13;