jQuery:尝试使用prepend()

时间:2012-03-11 14:31:37

标签: jquery prepend

我正在尝试使用prepend()在输入文本之前放置文本。

这是我的尝试:

http://jsfiddle.net/sx7JW/2/

但它不起作用..

任何想法?

哈维

2 个答案:

答案 0 :(得分:2)

您不想使用prepend,它会在输入中添加给定文本,使您的文本出现在输入中。类似的东西:

<input type='text'>Your text</input>

您需要先使用之前创建

Your Text <input type='text'/>

请参阅此jsFiddle

您也可以执行此类操作,这会将文本添加为​​输入父项的第一项。

$('input').keypress(function(e) {
    if(e.keyCode == 13) {
        console.log($(this));
        $(this).parent().prepend("<p>fasf</p>");
        alert('You pressed enter!');
    }
  });

答案 1 :(得分:0)

  1. 你把它设置为mootools而不是jquery
  2. 你需要的是之前()不是前置()。
  3. http://jsfiddle.net/sx7JW/9/

相关问题