0
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
</head><
<body>
    <button onClick="change()">Change Input</button>
    <div>
        <input value="It works">
    </div>

<script>
    function change() 
    {
        $('div').html('<input value="it didnt work">');
    }

    $('input').focus(function () 
    {
        if (this.value == this.defaultValue) 
        {
            this.value = '';
        }
    }).blur(function() 
    {
        if (this.value == '') 
        {
            this.value = this.defaultValue;
        }
    });
</script>

</body>
</html>

ここでは、divのhtmlを変更した後、入力に焦点を合わせようとします。しかし、それはうまくいきませんでした、なぜですか?

4

1 に答える 1

0

$('input').focus();入力ボックスにフォーカスするために使用します。入力ボックスがすでに dom に存在することを確認してください。

于 2013-03-17T20:42:28.360 に答える