当javascript在服务器上运行时未定义

时间:2013-06-13 05:19:00

标签: javascript angularjs

这让我难以理解以下代码:

<!DOCTYPE html>
<html ng-app>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script>
function ctrl($scope){
    $scope.submit = function(e){
        var elem = angular.element(e.srcElement);
        alert($(elem.parent()).html());
        //alert ('first name ' + person.first + ' last name ' + person.last);
    };
}
</script>
</head>
<body>
<div ng-controller="ctrl">
First: <input name="first" value="will" />
Last: <input name="last" value = "kriski" />
    <input type="submit" name="submit" ng-click="submit($event)"/>
</div>
</body>
</html>

在jsfiddle和plunker中工作正常(单击提交但在警报中显示HTML)。

但是当我在本地和我的个人服务器上运行相同的代码时,警报显示为“未定义”

这两项都有效: http://jsfiddle.net/sjmcpherso/yQs8z/193/ http://plnkr.co/qaqymNFQIe3ziyj7AKXa

我个人服务器上的相同粘贴代码没有 http://sjmcpherson.com/testing/test.html

真的很困惑

2 个答案:

答案 0 :(得分:1)

尝试使用此代码

<script type = "text/javascript">
    $(document).ready(function(e) {
        function ctrl($scope)
        {
            $scope.submit = function(e){
                var elem = angular.element(e.srcElement);
                alert($(elem.parent()).html());
                //alert ('first name ' + person.first + ' last name ' + person.last);
            };
        }
    });
</script>
在关闭function ctrl($scope) { //Your code goes here }

之前

或写</body>

答案 1 :(得分:1)

实际上我意识到错误只发生在Firefox而不是Chrome中,并且与srcElement有关,后者与Firefox不兼容这篇文章解释了修复How can I make event.srcElement work in Firefox and what does it mean?