1

次のコードを使用して、入力 DOM オブジェクト自体を含む入力に関する情報を配列に格納しようとしています。

export class SelectionOtherInputDescriptor {
    constructor(public selectionName: string, public otherKey: any, public otherInputElement: HTMLInputElement) { }
}
export class SelectionOtherInputHelper {
    selectionsWithOther: { [selectionKey: string]: SelectionOtherInputDescriptor; } = {};
    getAllSelectionOthers() {
        var things = $("[" + ATT_SELECTION_OTHER_FOR + "]");           
        for (var i = 0; i < things.length; i++) {         
            var selectionName = $(things[i]).attr(ATT_SELECTION_OTHER_FOR);
            var desc = new SelectionOtherInputDescriptor(selectionName, 0, $(things[i]));
            this.selectionsWithOther[selectionName] = desc;
        };
    };
}

オンラインで

var desc = new SelectionOtherInputDescriptor(selectionName, 0, $(things[i]));

コンパイルエラーが発生します:

タイプ 'JQuery' にsetSelectionRangeタイプ 'HTMLInputElement' のプロパティがありません

SelectionOtherInputDescriptor自分のオブジェクトでやりたいことは配列内のオブジェクトに格納することだけ なのに、HTMLInputElementパラメーターにプロパティが必要なのはなぜですか。setSelectionRange

4

1 に答える 1

1

$(things[i])は JQuery オブジェクトを返しますが、クラスには HTML 要素が必要です。観察:

<input id="foo">
   ....
var x = $('#foo');
console.log(x); // 'JQuery'
console.log(x.get(0)); '<input id="foo">

図のように電話する必要がありますget。これは HTMLInputElement になるだけなので、結果を型アサートする必要もあります。<HTMLInputElement>($(things[i]).get(0))

于 2013-03-05T20:36:08.133 に答える