0

jqueryの初心者です..私はこのhtmlコードを持っています

<div id='input'>
<table>
    <tr>
    <td class="number">
    <input><input><input><input><br>
    </td>
    <td style="vertical-align:bottom"><button>+</button></td>
    </tr>
    <tr>
    <td class="number">
    <input><input><input><input><br>
    </td>
    <td style="vertical-align:bottom"><button>+</button></td>
    </tr>
</table>
</div>

そして私のスクリプト部分のために

jQuery.fn.vcenter = function(parent) {
    if (parent) {
        parent = this.parent();
    } else {
        parent = window;
    }
    this.css({
        "position": "absolute",
        "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px"),
    });
return this;
}
jQuery.fn.hcenter = function(parent) {
    if (parent) {
        parent = this.parent();
    } else {
        parent = window;
    }
    this.css({
        "position": "absolute",
        "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
    });
return this;
}
jQuery.fn.center = function(parent) {
    if (parent) {
        parent = this.parent();
    } else {
        parent = window;
    }
    this.css({
        "position": "absolute",
        "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
        "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px"),
    });
return this;
}
$(document).ready(function(){
$("#input").vcenter(true);
});
$("#input:button").click(this.parent().prev().html()+="<input><input><input><input><br>");

基本的にやろうとしているのは、IDが「input」の要素内のボタンをクリックするたびに
、スクリプトが次のように実行することです。スクリプトは親要素()に移動し、次に前の兄弟に移動します。次に、そのhtml内部に次のように追加します。"<input><input><input><input><br>"
宿題を済ませました。そして私は助けが必要です!ここで何が問題になっていますか?

4

2 に答える 2

0

.click()ハンドラーは、パラメーターとして関数を取ります。

それで

$("#input button").click(this.parent().prev().html()+="<input><input><input><input><br>");

になります

$("#input button").click(function(event) {
    var old_html = $(this).parent().prev().html();
    $(this).parent().prev().html(old_html+"<input><input><input><input><br>");
});
于 2013-01-04T20:46:13.717 に答える
0

構文エラーがあります。試す :

$("#input:button").click(function(
    //$(this) or $(".number") depending on your need
    $(this).parent().prev().html(
        $(this).parent().prev().html()+="<input><input><input><input><br>")
    );
});
于 2013-01-04T20:46:28.047 に答える