简体   繁体   English

使用jQuery document.ready插入HTML

[英]Inserting HTML with jQuery document.ready

I'm a beginner at jQuery and I'm trying to insert a Facebook like button through the jQuery document.ready function. 我是jQuery的初学者,我试图通过jQuery document.ready函数插入一个类似Facebook的按钮。

My external Javascript file (loaded after the jQuery script) has the following code: 我的外部Javascript文件(在jQuery脚本之后加载)具有以下代码:

$(document).ready(function(){

if ($('#fb_btn').length) {
  var fb_code = "";

  fb_code += "<iframe src=\"http://www.facebook.com/plugins/like.php?href=" + escape(document.URL) +"&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:450px; height:80px;\" allowTransparency=\"true\" >";

  $('#fb_btn').prepend(fb_code);
}

});

My HTML code for where I want the button is 我想要按钮的HTML代码是

<span id="fb_btn"></span>

Currently, nothing seems to load into the span . 目前,似乎没有任何东西加载到span

I'm trying to use this instead of inserting the code directly because inserting directly slows down the page down a bit too much. 我正在尝试使用它而不是直接插入代码,因为直接插入会使页面减慢太多。

Thanks. 谢谢。

Brian, the errors you're getting on your page seem to be related to the absence of the $ variable. Brian,您在页面上遇到的错误似乎与$变量的缺失有关。 Are you sure you're running your $(document).ready() only after you load your jQuery scripts? 您确定仅加载jQuery脚本后才运行$(document).ready()吗? Remember...Javascript runs from top to bottom, so if you load your jQuery script after you run anything on the $ or jQuery variables, you will get object undefined errors. 请记住... Javascript从上到下运行,因此如果在$jQuery变量上运行任何内容后加载jQuery脚本,则会出现对象未定义的错误。

Then, to reiterate what others have said, you should probably close that html string with </iframe> and put the iframe in a block-level element. 然后,为了重申其他人所说的内容,您应该使用</iframe>关闭该html字符串,并将iframe放在块级元素中。

I think I just fixed the problem by updating the jQuery on my site. 我想我只是通过更新我网站上的jQuery来解决问题。 It was version 1.4.2. 它是版本1.4.2。 By using Version 1.4.3, everything seems to work fine. 通过使用1.4.3版本,一切似乎都运行良好。

Either that or there was something modified incorrectly with the jQuery I had. 无论是那个还是有一些我用jQuery错误修改的东西。

Thanks for your help everyone. 谢谢大家的帮助。

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

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