我有我的li中的复选框,我希望li可以被dlbclick编辑,但它会删除checbox。我可以将复选框与我的文本值一起包含,但我不想这样做,还有其他方式吗?保留一些html元素而不替换新的相同html元素
$(document).on('dblclick', '#li', function() {
oriVal = $(this).text();
$(this).text("");
input = $("<input type='text' id='input'>");
input.appendTo($(this)).focus();
});
$(document).on('focusout', '#input', function() {
if ($(this).val() != "") {
newInput = $(this).val();
$(this).hide();
$(this).closest('li').text(newInput);
} else {
$(this).closest('li').text(oriVal);
}
});
HTML
<ul>
<li id="li">
<input type="checkbox" name="1" value="1">item 1</li>
<li id="li">
<input type="checkbox" name="2" value="2">item 2</li>
</ul>
thx这么多! var value = $ .trim(this.value), 为什么在这里你用这个而不是$(this)?和逗号而不是; ?? –
@HildyMaass,因为它是一个输入字段,您可以从dom引用本身访问该值,因此不需要使用[$(this).val()](http://api.jquery.com/val/)这里 –
@HildyMaass为了使用[var](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)来声明多个变量,你可以使用',' –