如何解决:HTML 模板必须包含单个根元素

我正在尝试将 onsen navigator 与多个“.html”文件一起使用,但我遇到了同样的错误,无论我尝试过什么

我的场景

  • -万维网

  • /脚本

  • 索引.html

  • 主页.html

我想从 index.html 转到 home.html

错误:

Uncaught (in promise) Error: [Onsen UI] HTML template must contain a single root element

    at Object.Q.throw (onsenui.min.js:2)

    at Object.Q.createElement (onsenui.min.js:2)

    at onsenui.min.js:2

我的 index.html:


    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">

    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">

    <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>


    <script src='scripts/loginScript.js' type='text/javascript'></script>


    <link rel="stylesheet" href="style.css">

    <title>Tu Psicologa de Cabecera</title>

</head>

<body>

    <ons-navigator id="navigator" page="index.html"></ons-navigator>

        <ons-template id="index.html">

            <ons-page id="index">

                <img class="imagenLogo" src="https://i.imgur.com/cq7BbWn.png">


                <ons-card class="loginCard">

                    <ons-input id="userLogin" modifier="underbar" placeholder="Usuario" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>

                    <ons-input id="passLogin" modifier="underbar" placeholder="Contraseña" type="password" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>


                    <ons-button class="loginButton" onclick="loginNomral()">ENTRAR</ons-button>

                    <ons-button class="loginFacebookButton">ENTRAR CON FACEBOOK</ons-button>

                    <label class="labelRegister">REGISTRARME</ilabel>

                </ons-card>

            </ons-page>

        </ons-template>

    <script type="text/javascript" src="cordova.js"></script>

</body>


墨色风雨
浏览 136回答 3
3回答

肥皂起泡泡

解决方案很简单,几个小时后,我删除了所有的 html 格式并保留了标签。由此:<!DOCTYPE html><html>&nbsp; &nbsp;<head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">&nbsp; &nbsp; <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">&nbsp; &nbsp; <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>&nbsp; &nbsp; <link rel="stylesheet" href="style.css">&nbsp; &nbsp; <title></title></head><body>&nbsp; &nbsp; <ons-page id="home">&nbsp; &nbsp; &nbsp; &nbsp; <ons-toolbar>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="left"><ons-back-button>Page 1</ons-back-button></div>&nbsp; &nbsp; &nbsp; &nbsp; </ons-toolbar>&nbsp; &nbsp; &nbsp; &nbsp; <p>This is the second page.</p>&nbsp; &nbsp; </ons-page></body></html>对此:<ons-page id="home">&nbsp; &nbsp; <ons-card class="loginCard">&nbsp; &nbsp; &nbsp; &nbsp; <ons-input id="userLogin" modifier="underbar" placeholder="Usuario" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>&nbsp; &nbsp; &nbsp; &nbsp; <ons-input id="passLogin" modifier="underbar" placeholder="Contraseña" type="password" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>&nbsp; &nbsp; &nbsp; &nbsp; <ons-button class="loginButton" onclick="logintest()">ENTRAR</ons-button>&nbsp; &nbsp; &nbsp; &nbsp; <ons-button class="loginFacebookButton">ENTRAR CON FACEBOOK</ons-button>&nbsp; &nbsp; &nbsp; &nbsp; <label class="labelRegister">REGISTRARME</ilabel>&nbsp; &nbsp; </ons-card></ons-page>

BIG阳

Onsen UI navigator 使用您指定页面内容的模板,您无需创建不同的 html 文件来指定不同的页面document.addEventListener('init', function(event) {&nbsp; var page = event.target;&nbsp; if (page.id === 'page1') {&nbsp; &nbsp; page.querySelector('#push-button').onclick = function() {&nbsp; &nbsp; &nbsp; document.querySelector('#myNavigator').pushPage('page2.html', {data: {title: 'Page 2'}});&nbsp; &nbsp; };&nbsp; } else if (page.id === 'page2') {&nbsp; &nbsp; page.querySelector('ons-toolbar .center').innerHTML = page.data.title;&nbsp; }});&nbsp; &nbsp;<!DOCTYPE html>&nbsp; &nbsp;<html>&nbsp; &nbsp;<head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">&nbsp; &nbsp; <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">&nbsp; &nbsp; <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>&nbsp; &nbsp; <link rel="stylesheet" href="style.css">&nbsp; &nbsp; <title>App</title></head><body>&nbsp; &nbsp; <ons-navigator swipeable id="myNavigator" page="page1.html"></ons-navigator><template id="page1.html">&nbsp; <ons-page id="page1">&nbsp; &nbsp; <ons-toolbar>&nbsp; &nbsp; &nbsp; <div class="center">Page 1</div>&nbsp; &nbsp; </ons-toolbar>&nbsp; &nbsp; <p>This is the first page.</p>&nbsp; &nbsp; <ons-button id="push-button">Push page</ons-button>&nbsp; </ons-page></template><template id="page2.html">&nbsp; <ons-page id="page2">&nbsp; &nbsp; <ons-toolbar>&nbsp; &nbsp; &nbsp; <div class="left"><ons-back-button>Page 1</ons-back-button></div>&nbsp; &nbsp; &nbsp; <div class="center"></div>&nbsp; &nbsp; </ons-toolbar>&nbsp; &nbsp; <p>This is the second page.</p>&nbsp; </ons-page></template></body></html>

30秒到达战场

从你的代码看来,你</head>没有开口<head>但主要问题是你不能有 2 个标签作为根,这意味着你需要用一个<html></html>标签包装它们像这样:<html>&nbsp; &nbsp;<head>&nbsp; &nbsp; &nbsp; <title>Page Title</title>&nbsp; &nbsp;</head>&nbsp; &nbsp;<body>&nbsp; &nbsp; &nbsp; <h1>My First Heading</h1>&nbsp; &nbsp; &nbsp; <p>My first paragraph.</p>&nbsp; &nbsp;</body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript