简体   繁体   English

Facebook Javascript如何使用JQuery在加载时显示变量

[英]Facebook Javascript How do you use JQuery to show the variable on load

How do I do the following trivial thing, using FBJS.... 我如何使用FBJS做以下琐碎的事情。

I have the following code... I'm trying to display the text in html on load: 我有以下代码...我正在尝试在加载时以html显示文本:

  var hero_text_under_photo = 'Hero 1'
  var hero_2_text_under_photo = 'Hero 2'
  var hero_3_text_under_photo = 'Hero 3'
  var hero_4_text_under_photo = 'Hero 4'

  console.log(hero_text_under_photo);
    $('#hero_1_text_under_photo').setTextValue(hero_text_under_photo);
    $('#hero_2_text_under_photo').setTextValue(hero_2_text_under_photo);
    $('#hero_3_text_under_photo').setTextValue(hero_3_text_under_photo);
    $('#hero_4_text_under_photo').setTextValue(hero_4_text_under_photo);

I can read the variables with console.log, but I don't see the values set until I click them. 我可以使用console.log读取变量,但是直到单击它们,我才能看到设置的值。 How can I fix this? 我怎样才能解决这个问题?

<div id="whatsNew">
    <h2>What's New</h2>
    <ul id="topics" class="topics">
        <li class="topic tab1 topicSelected" onclick="rotator.loadIndex(0); rotator.stop()">
            <p id="hero_1_text_under_photo"></p>
        </li>
        <li class="topic tab2" onclick="rotator.loadIndex(1); rotator.stop()">
  <p id="hero_2_text_under_photo"></p>
        </li>
        <li class="topic tab3" onclick="rotator.loadIndex(2); rotator.stop()">
              <p id="hero_3_text_under_photo"></p>
        </li>
        <li class="topic tab4" onclick="rotator.loadIndex(3); rotator.stop()">
              <p id="hero_4_text_under_photo"></p>

Put your code inside 将您的代码放入

$(document).ready(function() {
   //Code here
});

The code inside will be executed when the DOM is fully loaded DOM完全加载后,将执行内部代码

First, you should wrap your javascript code so that it gets executed on document ready. 首先,您应该包装JavaScript代码,以便在准备好文档时执行它。 You can do this with: 您可以执行以下操作:

$(function() { //define a function to execute on document ready
 //your code here
});

The next step is to use the correct jQuery method. 下一步是使用正确的jQuery方法。 setTextValue is not a method as far as I am aware; 据我所知, setTextValue不是一种方法; instead use text() , so the final code would be: 而是使用text() ,所以最终代码将是:

$(function(){
  var hero_text_under_photo = 'Hero 1'
  var hero_2_text_under_photo = 'Hero 2'
  var hero_3_text_under_photo = 'Hero 3'
  var hero_4_text_under_photo = 'Hero 4'

  console.log(hero_text_under_photo);
    $('#hero_1_text_under_photo').text(hero_text_under_photo);
    $('#hero_2_text_under_photo').text(hero_2_text_under_photo);
    $('#hero_3_text_under_photo').text(hero_3_text_under_photo);
    $('#hero_4_text_under_photo').text(hero_4_text_under_photo);
});

You can see a fiddle of this here . 您可以在这里看到一个小提琴。

Also, it's worth noting that you should avoid using onclick attributes to attach events to DOM nodes. 另外,值得注意的是,您应避免使用onclick属性将事件附加到DOM节点。 Instead use jQuery to programmatically hook-up events; 而是使用jQuery以编程方式连接事件; this keeps your HTML much cleaner, and follows the paradigm of unobtrusive javascript . 这可以使您的HTML更加整洁,并遵循不引人注目的javascript的范例。 You can do it like this: 您可以这样做:

$("#your_selector").click(function() {
    //your event handling code here
});

You can't use jQuery with FBJS. 您不能将jQuery与FBJS一起使用。 You must use document.getElementById(id).setTextValue(). 您必须使用document.getElementById(id).setTextValue()。

If you want to use jQuery, you will need to switch to an iframe Canvas. 如果要使用jQuery,则需要切换到iframe画布。

There is no method called setTextValue , use html or text method to set any content inside the dom element. 没有称为setTextValue方法,使用htmltext方法来设置dom元素内的任何内容。 Try this 尝试这个

$(function(){

 var hero_text_under_photo = 'Hero 1'
  var hero_2_text_under_photo = 'Hero 2'
  var hero_3_text_under_photo = 'Hero 3'
  var hero_4_text_under_photo = 'Hero 4'

  console.log(hero_text_under_photo);
    $('#hero_1_text_under_photo').text(hero_text_under_photo);
    $('#hero_2_text_under_photo').text(hero_2_text_under_photo);
    $('#hero_3_text_under_photo').text(hero_3_text_under_photo);
    $('#hero_4_text_under_photo').text(hero_4_text_under_photo);

});

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

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