3
function openFaceBox(path) {
    $.ajax({
        type: "GET",
        url: path,
        success: function( data ) {
            $.facebox( data ); // data returns html
                    var tableHeight = $(data).find('table').height();
                    console.log( tableHeight ); // Output : 0 (Zero)
        }               
    }); 
}

私のAJAX戻りhtmlは以下の通りです:

<div id="holder-1">
    <h1>Content 1</h1>
</div>
<div id="holder-2">
    <h1>Content 2</h1>
</div>
<div id="holder-3">
    <h1>Content 3</h1>
</div>
<table>
    <tr>
        <td>abcd</td>
        <td>Some Text Here Some Text Here Some Text Here Some Text Here
            Some Text Here Some Text Here Some Text Here Some Text Here Some Text
            Here Some Text Here Some Text Here Some Text Here Some Text Here Some
            Text Here Some Text Here Some Text Here Some Text Here Some Text Here
            Some Text Here Some Text Here Some Text Here Some Text Here Some Text
            Here</td>
    </tr>
</table>

なぜ機能しないのかわかり.find()ません。基本的にテーブルの高さを知りたいです。混乱があれば教えてください。

4

3 に答える 3

3

を使用しfilter()ます。

console.log($(data).filter('table'));

それdataがの文字列であると仮定すると、次のHTMLことができます。

$(data).find('table');

tableこれにより、データをに追加せずにが返されDOMます。

于 2013-03-08T11:17:05.350 に答える
1

あなたは次のようなことをする必要があります

reveal.faceboxイベントハンドラーを登録する

$(document).on('reveal.facebox', function(){
    var tableHeight = $('#facebox .content table').height();
    //Do whatever you want with the height
});

function openFaceBox(path) {
    $.ajax({
        type: "GET",
        url: path,
        success: function( data ) {
            $.facebox( data ); // data returns html
                    var tableHeight = $(data).find('table').height();
                    console.log( tableHeight ); // Output : 0 (Zero)
        }               
    }); 
}

デモ:フィドル

注:
$(data).find('table').height()この要素はまだdomにレンダリングされていないため、機能しません。したがって、この要素の高さ/幅はありません。フェイスボックスアイテムがレンダリングされると、フェイスボックスはイベントをトリガーしrevleal.facebox、フェイスボックスのコンテンツはデフォルトで#facebox .content要素にレンダリングされます。

于 2013-03-08T16:34:16.233 に答える
0

$ .facebox()が何であるかはわかりませんが、要素がDOMに読み込まれるまで、要素の高さを見つけることはできません。これはちょっとクレイジーですが、それはあなたの身長を取得する必要があります。

    $('body').append($('<div>',{class:'faceboxHolder'}).css('left':'5000px', 'position':'absolute').append(data));    

    var tableHeight = $('.faceboxHolder').find('table').height();

    $('.faceboxHolder').remove();
于 2013-03-08T16:17:15.067 に答える