开发者

Can someone tell me why it is failing?

$.each(['a'], function(){$('<a/>').text(this).appendTo('body');});

=> firefox and chrome told me that 'appendTo'开发者_JAVA技巧 is not a function...


You can use:

$.each(['a'], function(idx, elem){$('<a/>').text(elem).appendTo('body');});

Or even better, just use a for loop:

var elements = ['a'];
for (var i in elements) {
    $('<a/>').text(elements[i]).appendTo('body');
}

It's both easier to read and faster to execute.


.text() is a function which sets the text, but I don't think you can chain it together. Try opening up that line:

$('a').each(function() {
  var a = $('<a/>');
  a.text($(this));
  $('body').append(a);
});


$('<a/>') should this be $('a')


What are you trying to do?

Perhaps something more like? $('a').each(function(){$('body').append('')});


Doesn't the text function return a string object, not a jQuery object?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜