我想摆脱 “TypeError: $(...).live is not a function”
错误。代码如下,我已经尝试过但无法修复它。
$(".search-text input[data-default], .gdlr-comments-area input[data-default]").each(function() {
var t = $(this).attr("data-default");
$(this).val(t), $(this).live("blur", function() {
"" == $(this).val() && $(this).val(t)
}).live("focus", function() {
$(this).val() == t && $(this).val("")
})
最佳答案
以下是与等效语句执行相同操作的新方法和旧方法:
$( selector ).live( events, data, handler ); // jQuery 1.3+
$( document ).delegate( selector, events, data, handler ); // jQuery 1.4.3+
$( document ).on( events, selector, data, handler );
以及更多示例:
$( "a.offsite" ).live( "click", function() {
alert( "Goodbye!" ); // jQuery 1.3+
});
$( document ).delegate( "a.offsite", "click", function() {
alert( "Goodbye!" ); // jQuery 1.4.3+
});
$( document ).on( "click", "a.offsite", function() {
alert( "Goodbye!" ); // jQuery 1.7+
});
关于javascript - 如何将 .live() 转换为 .on(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458634/