我从 HTML 和 Javascript 开始,我打算收集 id 为“login”的文本输入的值,并在我按下“login”按钮但没有出现任何内容时显示它,我认为 javascript 无法识别我。这是 HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>login</title>
<script>
window.resizeTo(700,900);
</script>
</head>
<body>
<div class="app">
<table id=tabla style="width:100%">
<tr>
<th> <input type="text" class="input" id="login" placeholder="Su API_KEY de Trello"/> </th>
<div id="txt"></div>
</tr>
<tr>
<th> <form action="https://trello.com/app-key/">
<input type="submit" class="nobutton"value="Consiga su API_KEY aquí"/>
</form>
</th>
</tr>
<tr>
<th>
<input type="button" name="button"class="button" value="Login"onclick ="myfuncion()"/>
<p id="demo">hola</p>
</th>
</tr>
</table>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
这是 index.js
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
function myfuncion() {
var x = document.getElementById("login").value;
document.getElementById("demo").innerHTML = x;
}
};
实际上,您的问题是您的函数包含在对象的范围内,因此您还必须从 HTML 中引用该对象。
此外,您错误地定义了对象内部的函数。它应该是以下形式:
您更正的示例: