【发布时间】:2012-05-14 01:42:30
【问题描述】:
var positions = [ "00", "01", "02", "03", "04", "05" ];
jQuery.each( positions, function(players) {
var playerNotesContent = document.createElement('div');
playerNotesContent.setAttribute('id', 'player_stats_block'+this);
$("#player_stats_block" + this).append (columns);
})
我想知道,为什么.append 命令不能与+this 一起使用,但是在创建 div 时它可以工作。我究竟做错了什么?抱歉我的英语不好,提前非常感谢!
更新!
var positions = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18" ];
jQuery.each( positions, function(players, value) {
var player_id = $("#ctl" + value).attr("href").match(RegExp('players/([^/]+)/details.php$'))[1];
var playerlink = document.getElementById("ctl" + value);
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.test.com/players/item/' + player_id,
onload: function(responseDetails)
{
var page = jQuery(responseDetails.responseText);
var columns = jQuery('div.columns',page);
var playerNotesContent = document.createElement('div');
playerNotesContent.setAttribute('id', 'player_stats_block'+value);
playerlink.parentNode.insertBefore( playerNotesContent, playerlink.nextSibling );
$('#player_stats_block00').append (columns);
}
});
});
像这样,所有值都被附加到#player_stats_block00 中!我尝试了您的几个答案,但我无法将它们附加到正确的#player_stats_blockXX。再次抱歉我的英语不好,谢谢大家的帮助!
【问题讨论】:
-
你想用 this 做什么?如果这是数组的索引或值,则应使用回调函数提供的值: $.each(positions, function(index,value) {
-
@ThomasHaratyk:
this将是String,在第一种情况下是"00",在第二种情况下是"01",依此类推:api.jquery.com/jQuery.each