为什么我在以下场景中收到 jQuery 错误“TypeError: $(...).live is not a function”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19110839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I'm getting the jQuery error "TypeError: $(...).live is not a function " in following scenario?
提问by PHPLover
Actually I'm trying to implement the autocomplete functionality to one text field but getting the above error, couldn't understand why I'm getting this error. Can you help me in resolving this error? For your reference I'm providing all the necessary code below:
实际上,我正在尝试对一个文本字段实现自动完成功能,但出现上述错误,无法理解为什么会出现此错误。你能帮我解决这个错误吗?为了您的参考,我在下面提供了所有必要的代码:
report-student-result.tpl
报告-学生-结果.tpl
<link rel="stylesheet" type="text/css" href="{$control_css_url}ui-lightness/jquery-ui-1.10.3.custom.css">
<link rel="stylesheet" type="text/css" href="{$control_css_url}autocomplete.css">
<label>Name</label>
<div class="form-element" id="friends">
<input type="text" class="" name="user_name" id="user_name" value="{$user_name}" />
</div>
<script language="javascript" type="text/javascript">
$(function(){
var class_id = $('#class_id').val();
var section_id = $('#section_id').val();
//attach autocomplete
$("#user_name").autocomplete({
//define callback to format results
source: function(req, add){
//pass request to server
$.getJSON("report_student_result.php?callback=?op=get_student_names&class_id="+class_id+"§ion_id="+section_id, req, function(data) {
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//pass array to callback
add(suggestions);
});
},
//define select handler
select: function(e, ui) {
//create formatted friend
var friend = ui.item.value,
span = $("<span>").text(friend),
a = $("<a>").addClass("remove").attr({
href: "javascript:",
title: "Remove " + friend
}).text("x").appendTo(span);
//add friend to friend div
span.insertBefore("#user_name");
},
//define select handler
change: function() {
//prevent 'to' field being updated and correct position
$("#user_name").val("").css("top", 2);
}
});
//add click handler to friends div
$("#friends").click(function(){
//focus 'to' field
$("#user_name").focus();
});
//add live handler for clicks on remove links
$(".remove", document.getElementById("friends")).live("click", function(){
//remove current friend
$(this).parent().remove();
//correct 'to' field position
if($("#friends span").length === 0) {
$("#user_name").css("top", 0);
}
});
});
</script>
Please help me out to resolve this error. Thanks in advance.
请帮我解决这个错误。提前致谢。
回答by Jamie Hutber
live()
has been removed since version 1.9 and was deprecated since 1.7:
live()
自 1.9 版起已被移除,自 1.7 版起已弃用:
You'll be wanting on()
now days
你会想要on()
现在的日子
$('#friends').on("click", ".remove", document.getElementById("friends"), function(){
where #friends
is available on DOM ready. You can't bind on()
on a dynamical loaded element.
哪里#friends
可以在 DOM 上使用。您不能绑定on()
动态加载的元素。
回答by Ajinder Singh
You can use the .on()
instead of .live()
(deprecated after Jquery 1.7)
您可以使用.on()
代替.live()
(在 Jquery 1.7 后弃用)
$(document).on('event', 'selector', function() {});
replaces .live()
.
$(document).on('event', 'selector', function() {});
替换.live()
.
Eg:
例如:
$( document ).on( "click", "#elementId ", function(){ alert( "Do here what you want!" ); // jQuery1.7+ });
回答by Scary Wombat
Live() has been removed since 1.9 from jquery. Please use on
Live() 自 1.9 起已从 jquery 中删除。请用on