简体   繁体   English

element.blur()导致未捕获的TypeError:elem.getAttribute不是函数

[英]element.blur() causing Uncaught TypeError: elem.getAttribute is not a function

So I am struggling to understand where a console error is coming from. 因此,我努力了解控制台错误的来源。 It doesn't currently break any behavior, but the error is unsettling. 它目前没有中断任何行为,但是错误令人不安。 The error happens when I trigger a .blur() on a span that is being edited. 当我在正在编辑的跨度上触发.blur()时,会发生错误。 Jquery then complains "jquery.js:1451 Uncaught TypeError: elem.getAttribute is not a function" jQuery然后抱怨“ jquery.js:1451 Uncaught TypeError:elem.getAttribute不是一个函数”

The directive html: 指令html:

<span ng-if="!time">{{ value }}</span>
<button class="btn btn-primary delink" 
        ng-if="delinkable && delinkVisable" 
        ng-click="resource.delink()">Not speaking</button>
<span ng-if="time">{{ value | momentFormat }}</span>
<div ng-if="time && datepicker" class="dropdown" id="datepicker">
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
    <datetimepicker ng-model="resource.MeetingDateTime" 
                    data-on-set-time="updateResource()"
                    data-datetimepicker-config="{ dropdownSelector: '#dropdown2' }">
    </datetimepicker>
</ul>
<a class="dropdown-toggle" id="dropdown2" role="button" data-disabled="true"
   data-toggle="dropdown" data-target="#" href>
    <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
    </div>
</a>

The relevant link functions: 相关链接功能:

function handleChanges () {
    var editSpan = angular.element(element.children()[0]);
    var newValue;

    if (scope.time) {
        newValue = dates.makeDateTime(editSpan.text());
        if (newValue) {
            scope.resource[scope.property] = newValue;
            newValue = editSpan.text();
        } else {
            editSpan.text(oldValue);
            newValue = editSpan.text();
            scope.resource[scope.property] = dates.makeDateTime(editSpan.text());
            scope.datepicker = true;
            $('#dropdown2').trigger('click');
            scope.$digest();
        }
    } else {
        scope.resource[scope.property] = editSpan.text();
        newValue = editSpan.text();
    }

    if (newValue !== oldValue) {
        scope.updateResource();
        compileElement();
    } else {
        if (scope.time) {
           hideDatepicker();
        }
    }
}

function bindEvents () {
    var editSpan = angular.element(element.children()[0]);

    editSpan.on('blur', function () {
        editSpan.attr('contentEditable', false);
        editSpan.unbind('keydown');
        editSpan.unbind('blur');
        scope.editing = false;
        handleChanges();
    });
    editSpan.on('keydown', function(event){
        if (event.keyCode == 27) {
            event.preventDefault();
            editSpan.text(oldValue);
            editSpan.blur(); //The error seems to happen after this call
        } else if (event.keyCode == 13) {
            editSpan.blur(); //and after this call
        } else if (scope.deleteable && event.keyCode == 8) {
            if (editSpan.text().length == 0 || event.ctrlKey) {
                scope.delete();
            }
        }
    });
}

So I don't know how generalizable this answer will be, but in case it is useful to you, the problem occurred because of the compile function. 因此,我不知道该答案的通用性,但是如果对您有用,那么问题就出在编译功能上。

Basically, compileElement was replacing the originally bound instance of the span with the freshly compiled instance, so when Jquery eventually got around to trying to rebind the click function, the element no longer existed, even though an identical span was there in it's place. 基本上,compileElement用新编译的实例替换了span的原始绑定实例,因此,当Jquery最终尝试重新绑定click函数时,该元素不再存在,即使它所在的范围相同。

The solution was to update the values of the original span, instead of using $compile to re-compile the element. 解决方案是更新原始范围的值,而不是使用$ compile重新编译元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JS get元素的elem.getAttribute(attr)和elem [attr]之间的区别 - difference between JS get element's attribute with elem.getAttribute(attr) and elem[attr] 有没有办法知道“ blur”是由“ element.blur()”调用还是“实际”模糊? - Is there a way to know if “blur” was called by “element.blur()” or if it was “actual” blur? 测试在element.blur()上运行函数之前是否单击了按钮 - test whether button being clicked before running function on element.blur() 未捕获的TypeError:elem.replace不是函数 - Uncaught TypeError: elem.replace is not a function 未捕获的TypeError:elem.nodeName.toLowerCase不是函数Jquery - Uncaught TypeError: elem.nodeName.toLowerCase is not a function Jquery 是什么导致错误“未捕获的TypeError:数字不是函数” - What is causing the error “Uncaught TypeError: number is not a function” 是什么导致Uncaught TypeError:number不是函数? - What is causing Uncaught TypeError: number is not a function? setTimeout导致未捕获的TypeError:number不是函数 - setTimeout causing Uncaught TypeError: number is not a function TypeError:elem.autocomplete不是函数 - TypeError: elem.autocomplete is not a function 未捕获的类型错误:elem.getClientRects 不是在 MVC 上使用多选时的函数 - Uncaught TypeError: elem.getClientRects is not a function when using multiple select on MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM