1

1 つの jquery オブジェクトに入れたい要素の配列があります。jquery.add() が jquery オブジェクトに要素を追加していないようです。私が追加しているものは実際には html 要素であることを示すコンソール ログがありますが、まだ追加されていません。

私のコード:

console.log('iterating')
console.log(content)
//add the content into the jquery item, and then we can load it into the qtip
_.each(additionalContent, function(value, element, list){
console.log('adding')
console.log(value)
console.log(content.length)
content.add(value) //trying content.add($(value)) gives the same result/output
console.log(content.length)
})

console.log('asdf')
console.log(content.length)
console.log(content)

=========================== 以下を出力します。

iterating 
[p.nonVendor unselectable, selector: "", context: undefined, jquery: "1.9.1", constructor: function, init: function…]
adding 
<button class type=​"button" data-loading-text=​"OK" style=​"text-align:​ center;​ font-style:​ normal;​ font-variant:​ normal;​ font-weight:​ normal;​ font-size:​ 13px;​ line-height:​ normal;​ font-family:​ arial;​ color:​ rgb(255, 255, 255)​;​ background-color:​ rgb(0, 0, 255)​;​ z-index:​ 2;​ display:​ inline;​ left:​ auto;​ top:​ auto;​ width:​ 35.77777862548828px;​ height:​ 17.777777671813965px;​">​OK​&lt;/button>​
1 
1 
adding
<button class type=​"button" data-loading-text=​"Cancel" style=​"text-align:​ center;​ font-style:​ normal;​ font-variant:​ normal;​ font-weight:​ normal;​ font-size:​ 13px;​ line-height:​ normal;​ font-family:​ arial;​ color:​ rgb(255, 255, 255)​;​ background-color:​ rgb(0, 0, 255)​;​ z-index:​ 2;​ display:​ inline;​ left:​ auto;​ top:​ auto;​ width:​ 35.77777862548828px;​ height:​ 17.777777671813965px;​">​Cancel​&lt;/button>​
1 
1 
asdf 
1 
[p.nonVendor unselectable, selector: "", context: undefined, jquery: "1.9.1", constructor: function, init: function…]
4

2 に答える 2

3

.add() は元の変数を変更しないため、次のように結果を 'content' に戻す必要があります。

content = content.add($(value));
于 2013-10-26T15:39:03.297 に答える
0

contentのオブジェクトと次のコードの両方を含む JavaScript 配列を作成するにはadditionalContent、次のコードが機能します。

elements = [];
content.each(function(index, value) {
    elements.push(value);
});
additionalContent.each(function(index, value) {
    elements.push(value);
});

いくつかのポイント:

  • 各関数の最初の引数はインデックスでロードされます (for ループを使用するのと同様: for( int index = 0; index < content.length; index++ )、2 番目の引数は要素です
  • jQuery メソッド .add は、オブジェクトのコレクションではなくセレクターの処理に使用され、コードに必要な方法でまったく動作しません。

その後elements、コレクションをオブジェクトで使用できます - これが必要ですか?

編集 - Jquery セレクターを連結するには:

wdosanjos が指摘しているように、次のように動作します - 必要ありませ.eachん:

content = content.add(additionalContent);

css クラスのソーセージを両方のセレクターのすべての要素に追加するには、次のようにします。

content.add(additionalContent).addClass('sausage');
于 2013-10-26T15:54:31.137 に答える