我正在尝试使用 on 函数在跨度和输入文本字段之间切换,但我需要一些帮助来找出我做错了什么。
我为它创建了一个 fiddle here
用户界面
<div>
<span >My value here!</span>
</div>
JS
var focusInName = '';
var focusOutName = '';
$(function () {
$("document").on("click",".span", function () {
var input = $('<input />', {'type': 'text', 'name': 'aname', 'value': $(this).html()});
$(this).parent().append(input);
$(this).remove();
input.focus();
focusInName = $(this).html();
alert(focusInName);
});
$('document').on('blur',".input", function () {
$(this).parent().append($('<span />').html($(this).val()));
$(this).remove();
focusOutName = $(this).val();
alert(focusOutName);
});
});
最佳答案
您应该将jquery的版本更改为1.9 并将类更改为 html 元素,如下所示:
$(document).on("click","span", function () {
//codes
}
输入字段也是如此:
$(document).on('blur',"input", function () {
//codes
}
编辑
如果您想将类设置为 html 元素,您可以执行以下操作:
var focusInName = '';
var focusOutName = '';
$(function () {
$(document).on("click",".sp", function () {
var input = $('<input class="box" />', {'type': 'text', 'name': 'aname','value': $(this).html()});
$(this).parent().append(input);
$(this).remove();
input.focus();
focusInName = $(this).html();
alert(focusInName);
});
$(document).on('blur',".box", function () {
$(this).parent().append($('<span class="sp" />').html($(this).val()));
$(this).remove();
focusOutName = $(this).val();
alert(focusOutName);
});
});
关于javascript - 在span标签上使用on函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555126/