问题描述:

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);

})

I would like to know, why the .append command doesn't work with the +this, but while creating div it works. What am I doing wrong? Sorry for my bad English and thanks a lot in advance!

UPDATED!

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);

}

});

});

Like this all values gets appended in #player_stats_block00! I tried several of your answers but i cant get them appended to their correct #player_stats_blockXX. Sorry again for my bad english and thank you all for your help!

网友答案:

Your code is creating a div and setting its id, and then trying to look up that div by that ID. The lookup will fail, because the div hasn't been added to the DOM anywhere yet.

var positions = [ "00", "01", "02", "03", "04", "05" ];

jQuery.each( positions, function(players) {
    // Here you're creating the div
    var playerNotesContent = document.createElement('div');

    // ...and setting its `id` (btw, you can just do playerNotesContent.id = value;)
    playerNotesContent.setAttribute('id', 'player_stats_block'+this);

    // ...and here trying to look it up
    $("#player_stats_block" + this).append (columns); 
});

If you're trying to append the div to columns, you probably want this instead of your last line:

columns.append(playerNotesContent);

(Note that append adds content to the container you call it on.)

That assumes columns is a jQuery object. Otherwise:

$(playerNotesContent).appendTo(columns);

or

$(columns).append(playerNotesContent);

(Note that appendTo adds the content you call it on to the argument you give it; it's the converse of append.)

It's also worth noting that the divs being created have no content, so unless you're styling them, they won't have any dimensions and will be kinda hard to find on the page. :-)

Here's an example assuming columns is a jQuery object, and giving the divs some content so you can see them: Live copy | source

HTML:

<div id="columns"></div>

JavaScript:

jQuery(function($) {

  var columns = $("#columns");
  var positions = [ "00", "01", "02", "03", "04", "05" ];

  jQuery.each( positions, function(players) {
    var playerNotesContent = document.createElement('div');
    playerNotesContent.innerHTML = "Player Stats";
    playerNotesContent.id = 'player_stats_block'+this;
    columns.append(playerNotesContent);
  });  

});
网友答案:

There are few things I would change. The callback provides the value as the second argument, not the first. Your call to append is followed by a space - which breaks. I'm not sure if you want to add columns to your new div, or the other way around.

var positions = [ "00", "01", "02", "03", "04", "05" ];

$.each( positions, function( index, value ) {
  $( "<div>", { id : 'player_stats_block' + value } )
    .appendTo( "#columns" );
});

If columns is a legitimate reference to a dom element to begin with, you can put that back into the call to $.appendTo() at the end of the primary line. It was unclear as to what this variable represented in your original post.

网友答案:
var positions = [ "00", "01", "02", "03", "04", "05" ];

jQuery.each( positions, function(index, value) {
   $('<div id="player_stats_block'+ value +'"></div>').append(columns);
});
网友答案:

replace this with $(this) -->

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); 
})
相关阅读:
Top