jQuery只在创建对象时调用函数

时间:2013-06-10 19:12:55

标签: javascript jquery

我正在使用FitText https://github.com/davatron5000/FitText.js调整特定DIV中某些文本的文本大小。事实是,只有在需要时才会将这些DIV创建为添加到页面中的对象。但是这样做FitText在页面加载时不会得​​到DIV,因为实际上DIV还没有,所以不会调整文本的大小。

以下是我如何调用FitText:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        jQuery(".cvd_right_master_item").fitText(2.8);
    });
</script>

这是将DIV创建为对象的函数:

function makeCvdBubble(params)
    {  
    var cvdIndexId = params.cvdIndexId;
    var cvdTweetAuthor = params.cvdTweetAuthor;
    var cvdAuthorName = params.cvdAuthorName;
    var cvdTweetDescription = params.cvdTweetDescription;

    objectreference = document.createElement('div');
    objectreference.setAttribute('id', 'indexId-'+cvdIndexId.toString());


    $(objectreference).html('\
    <div class="cvd_right_master_item">\
                    <div class="cvd_right_tweet_margin_float">\
                        <img class="cvd_right_blue_bird" src="cross_video_day/images/cvd_blue_bird.png" /><span class="cvd_right_tweet_username">@UserName</span>\
                        <span class="cvd_right_tweet_author">User Name</span>\
                        <span class="cvd_right_tweet_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ut posuere justo, eget consequat tortor. In sollicitudin condimentum arcu sed.</span>\
                    </div>\
                    <div class="clear_both"></div>\
                    <span class="cvd_right_white_arrow"></span>\
                </div>\
');

    if (cvdTweetAuthor) $(objectreference).find('.cvd_right_tweet_username').html(cvdTweetAuthor);
    if (cvdAuthorName) $(objectreference).find('.cvd_right_tweet_author').html(cvdTweetAuthor);
    if (cvdTweetDescription) $(objectreference).find('.cvd_right_tweet_text').html(cvdTweetDescription);

    return (objectreference) //objectreference is the reference of the just created tweet
    }

现在,如何在创建对象时使FitText调整文本大小? 谢谢!

1 个答案:

答案 0 :(得分:0)

将对象附加到DOM后。

试试这个:

   var myObj = $("TheParentDOMObject").appendChild(makeCvdBubble(params));
   myObj.fitText(2.8);