简体   繁体   English

JQuery .prependTo('body')不工作?

[英]JQuery .prependTo('body') not working?

It's midnight and I should just go to bed but I am utterly bamboozled. 现在是午夜,我应该去睡觉,但我完全是竹子。 I know I'm going to feel like a total egg here, but...why isn't this working?? 我知道我在这里会感觉像一个鸡蛋,但是......为什么这不起作用?

var galleryPanelText = '';
galleryPanelText += '<div class="galleryPanel">';
galleryPanelText += '</div>';
alert(galleryPanelText);
galleryPanelText.prependTo('body');
alert($('.galleryPanel').length);

http://jsfiddle.net/6kjKE/ http://jsfiddle.net/6kjKE/

As you can see, the first alert fires and the second doesn't, so the prepend line is breaking it. 正如您所看到的,第一个警报会触发而第二个警报不会触发,因此前置行会打破它。 Thanks in advance. 提前致谢。

You are prepending string but prependTo() needs an jQuery object to prepend like, 你正在prepending 字符串,prependTo()需要一个jQuery对象prepend

$(galleryPanelText).prependTo('body');

Also add a latest version of jquery see Working Fiddle 另外添加最新版本的jquery请参阅Working Fiddle

(1) In your fiddle you forgot to include jQuery ! (1)在你的小提琴中,你忘了包含jQuery

(2) It should be $(galleryPanelText).prependTo('body'); (2)它应该是$(galleryPanelText).prependTo('body');

See update fiddle: http://jsfiddle.net/6kjKE/1/ 请参阅更新小提琴: http//jsfiddle.net/6kjKE/1/

If you notice the console of your fiddle, it would show you: Uncaught TypeError: Object has no method 'prependTo' . 如果您注意到小提琴的控制台,它会显示: Uncaught TypeError:Object没有方法'prependTo' This would give you a hint. 这会给你一个提示。

If you include jQuery and run, the console wil log: Uncaught TypeError: Object <div class="galleryPanel"></div> has no method 'prependTo' which means it is expecting a jQuery object, not text. 如果你包含jQuery并运行,那么控制台将会记录: Uncaught TypeError:Object <div class="galleryPanel"></div>没有方法'prependTo' ,这意味着它需要一个jQuery对象,而不是文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM