我一直在試圖解決這個問題,我使用了一些我需要在客戶端的節點模塊,因此我使用browserify來生成build.js文件。 當我使用這個文件,我得到:在browserified javascript中找不到事件中的元素和函數
遺漏的類型錯誤:無法讀取空對值
和
財產「值」未捕獲的ReferenceError:[FUNCTION_NAME]是不是功能上的定義按鈕點擊事件..
當我試圖將事件從內聯「onclick」移動到someButton.addEventListener('click',函數(e))時,沒有任何反應,甚至沒有警報的作品。
它與此問題中描述的問題類似: Using browserify, Uncaught ReferenceError: function is not defined,但解決方案沒有幫助。
這裏是我的JavaScript文件,我使用創建使用browserify(outBrowserify.js)構建:
var serverName = "http://127.0.0.1:9000";
var socket = io.connect(serverName);
console.log('CLient connecting to server: ' + serverName);
socket.on('ConnectionConfirmation', function(data){
console.log('Message from server: ' + data.text);
});
var Firebase = require('firebase');
var btnLogin = document.getElementById('btn_validateLogin');
if(btnLogin)
{
btnLogin.addEventListener('click', function(e){ //nothing happens..
alert('function added..');
});
}
var username = document.getElementById('ipt_username');
console.log('username : ' + username.value); //gives an error
function ValidateLoginCredentials()
{
var username = document.getElementById('ipt_username');
var passwd = document.getElementById('ipt_password');
console.log(username.value);
console.log(passwd.value);
ClientID = username.value;
socket.emit('ValidateLogin', {username: username.value, password: passwd.value});
}
和HTML是:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="/javascripts/outBrowserify.js"></script>
</head>
<body>
<div id="login">
<table>
<tr>
<td>
<label>Username :</label>
</td>
<td>
<input id="ipt_username" type="text" name="username">
</td>
</tr>
<tr>
<td>
<label>Password :</label>
</td>
<td>
<input id="ipt_password" type="password" name="password">
</td>
</tr>
<tr>
<td colspan="2">
<input id="btn_validateLogin" type="button" value="Validate Login" onclick="ValidateLoginCredentials()">
</td>
</tr>
</table>
</div>
</body>
</html>
的HTML是從指數轉換.jade在我的應用程序中,因爲我使用了express.js。另外,當我直接運行腳本時,它可以工作,除了瀏覽器不可用的模塊。
如果你在DOM渲染後綁定按鈕,它會工作嗎? '$(document).ready(function(){bindBtnLogin()})' – Plato