所以,我现在坐在这里已经有一段时间了,试图弄清楚如何访问我的Login-Forms输入元素。
我正在编写基于聚合物入门套件的PWA,这是我的html代码:
<template id="loginTemplate">
<style>
//removed styles for better overview
</style>
<hgroup>
<h1>Login</h1>
</hgroup>
<form id="loginForm">
<div class="group">
<input id="username" type="email"><span class="bar"></span>
<label>Email</label>
</div>
<div class="group">
<input id="password" type="text"><span class="bar"></span>
<label>Passwort</label>
</div>
<button type="button" class="button buttonBlue" onclick="login()">Login
<div class="ripples buttonRipples"><span class="ripplesCircle"></span></div>
</button>
</form>
</template>
这是我的javascript(用同一个HTML文件编写):
<script>
// Define the element's API using an ES2015 class
class MyLogin extends Polymer.Element {
static get is() { return 'my-login'; }
}
var login=function()
{
var client = new HttpClient();
//var temp = document.querySelector('#loginTemplate');
//var formElements = temp.content.querySelectorAll("form")[0].elements;
var formElements = document.getElementById("loginForm").elements;
var pass=formElements["password"];
var mail=formElements["username"];
//have already tried:
//var pass=document.getElementById("password");
//var pass=document.getElementById("username");
//and
//var pass=$("#password").val();
//var pass=$("#username").val();
client.login('http://localhost:50253/Token', pass, mail, function() {
window.alert("logged in!")
client.get('http://localhost:50253/api/Task', function(response){
window.alert(response);
});
});
}
customElements.define(MyLogin.is, MyLogin);
</script>
问题是,每当我尝试使用方法&#34; getElementById&#34;时,我都会得到一个未定义的元素:
f.e。如果我使用document.getElementById("loginForm").elements;
我会收到此错误:
编辑:我不知道是否重要,但我想指出WHOLE html文件位于dom-module中未捕获的TypeError:无法读取属性&#39;元素&#39;为null
任何帮助表示赞赏!
答案 0 :(得分:1)
<!DOCTYPE html>
<html>
<body>
<style>
/*!doctype CSS*/
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
margin:0;
padding:0;
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}
body {
line-height:1;
}
article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section {
display:block;
}
nav ul {
list-style:none;
}
blockquote, q {
quotes:none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content:'';
content:none;
}
a {
margin:0;
padding:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
}
/* change colours to suit your needs */
ins {
background-color:#ff9;
color:#000;
text-decoration:none;
}
/* change colours to suit your needs */
mark {
background-color:#ff9;
color:#000;
font-style:italic;
font-weight:bold;
}
del {
text-decoration: line-through;
}
abbr[title], dfn[title] {
border-bottom:1px dotted;
cursor:help;
}
table {
border-collapse:collapse;
border-spacing:0;
}
/* change border colour to suit your needs */
hr {
display:block;
height:1px;
border:0;
border-top:1px solid #cccccc;
margin:1em 0;
padding:0;
}
input, select {
vertical-align:middle;
}
/*----------MY CODE----------*/
.wrapper {
width: 960px;
margin: auto;
}
a {
text-decoration: none;
}
/*-----NOT IN USE-----*/
.wrapperart {
width: 960px;
margin: auto;
border-width: 0px 5px 0px 5px;
border-style: solid;
}
/*-----NOT IN USE-----*/
/*-----Header-----*/
header {
width: 100%;
height: 120px;
background: #222;
}
header img {
width: 240px;
height: 120px;
float: left;
}
header nav {
float: right;
}
header nav ul {
margin-top: 40px;
}
header nav ul li {
display: inline-block;
margin-left: 20px;
list-style: none;
}
header nav ul li a {
font-family: arial;
font-size: 15px;
color: white;
position: relative;
}
nav ul li a:hover {
background-color: black;
}
nav ul li div{
display: none;
}
nav ul li:hover div{
position: absolute;
display: block;
float: left;
}
/*-----body-----*/
.mainbody {
width: 50%;
height: 800px;
background-color: #ccc;
overflow: hidden;
font-weigt: 400;
font-family: arial;
font-size: 20px;
text-align: center;
float: left;
}
div.mainbody div {
padding: 30px 0px;
}
.mainbody h1 {
font-weight: 400;
font-family: arial;
font-size: 25px;
text-align: center;
}
.luisterdiv {
width: 120px;
height: 150px;
background-color: #333;
z-index: 0;
}
.sidebarleft {
width: 25%;
height: 800px;
background-color: #ccc;
float: left;
overflow: hidden;
}
div.sidebarleft div {
height: 800px;
border-right: 2px solid black;
}
.sidebarright {
width: 25%;
height: 800px;
background-color: #ccc;
float: right;
}
div.sidebarright div {
width: 470px;
height: 800px;
border-left: 2px solid black;
overflow: hidden;
}
.slides {
display: block;
width: 940px;
height: 800px;
margin: 0;
padding: 0;
}
.slide {
float: left;
list-style-type: none;
width: 470px;
height: 800px;
}
li img {
height: 800px;
width: 470px;
float: left;
}
/*-----footer-----*/
footer {
width: 100%;
background-color: #222;
padding: 60px 0px;
clear: both;
}
footer nav {
float: left;
}
footer nav ul {
color: white;
}
footer nav ul li {
display: inline-block;
margin-left: 20px;
list-style: none;
}
footer nav ul li a {
font-family: arial;
font-size: 15px;
color: white;
}</style>
<!doctype html>
<html>
<head>
<title>VeluweAM</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<header>
<div class="wrapper">
<img src="http://openclipart.org/image/300px/svg_to_png/4112/Clue_Simple_Clouds.png">
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="programma.html">Programma</a></li>
<li><a href="luisteren.html">Luisteren</a>
<div class="luisterdiv">
<ul>
<li><a href="http://93.190.142.179:8126/stream.html">In Browser</a></li>
<li><a href="https://tunein.com/radio/VeluweAM-s299064">Tunein</a></li>
<li><a href="http://panel.beheerstream.com:2199/tunein/veluweam.pls">Winamp</a></li>
<li><a href="http://panel.beheerstream.com:2199/tunein/veluweam.asx">Windows Media Player</a></li>
</div>
</li>
<li><a href="over ons.html">Over ons</a></li>
<li><a href="onze dj.html">Onze DJ's</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</div>
</header>
<div class="sidebarleft">
<div>
<ul class="slides">
<li class="slide"><img src="http://openclipart.org/image/300px/svg_to_png/4112/Clue_Simple_Clouds.png"></li>
<li class="slide"><img src="http://openclipart.org/image/300px/svg_to_png/4112/Clue_Simple_Clouds.png"></li>
</ul>
</div>
</div>
<div class="mainbody">
<div class="mainbodydiv">
<article>
<h1>Welcome op VeluweAM, Nieuws uit de regio.</h1>
<p>het radio stations die voor alles wat heeft.</p>
<p>heeft u nog niet gezien wat wij in huis hebben?<br>
bekijken ons programma, ik kan u garanderen dat u iets vind!</P>
</article>
</div>
</div>
<div class="sidebarright">
<div>
</div>
</div>
<footer>
<div class="wrapper">
<nav>
<ul>
©2017 VeluweAM
<li><a href="index.html">Home</a></li>
<li><a href="programma.html">Programma</a></li>
<li><a href="Luisteren.html">Luisteren</a>
<li><a href="over ons.html">Over ons</a></li>
<li><a href="onze dj.html">Onze DJ's</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</div>
</footer>
</body>
</html>
</body>
</html>
内部的内容是影子dom的一部分,无法像访问“常规”Dom那样访问。
在你的情况下(Polymer v2)你需要做
<template>
修改强>
现在我意识到...函数登录必须在类中,你在外面。应该是这样的:
var formElements = this.shadowRoot.querySelector('#loginForm').elements;