动态附加文本行
代码的目标是在每次聚焦最后一行时创建一个新的输入行,直到达到一定数量的行。 我得到了这个解决方案 ,效果很好:
$(function() { $("#qc>div:last-of-type>input").live('focus', function() { $(this).parent().after($(this).parent().clone().attr('id', 'ansInput' + $('#qc>div').length).find('input').val('').end()); }); });
但我也希望每次更改行前的文本,因此clone
不会删除它
我修改它以编写预先准备好的HTML行,但它不起作用:
function questionsForm() { $("#qc>div:last-of-type>input").live('focus', function() { lineNum = $('#qc>div').length newLine = ("Answer {0}: ").format(lineNum); if ($('#qc>div').length <= 4) { $(this).parent().after(newLine); } }); }
( .format
方法是预定义的)
我想了解为什么我的代码不起作用,以及如何对其进行分析,但不是完全不同的解决方案(除非我的代码当然有一些基本的错误……)
你在lineNum = $('#qc>div').length
之后缺少一个分号lineNum = $('#qc>div').length
为什么askForm()函数中的代码?
你可以把它放在jquery ready函数$(function(){ ... });
另外你的格式function是什么样的?