1

PHPドキュメントからテキストを取得するための以下のコードがあります:

$.get("server/test.php", function(data){
    $('#thumbdest').prepend(data);
    // $('#thumbdest').html(data);
});

コメント行はすべてを期待どおりに返しますが、prependデータは返しません。挿入するとHTMLタグが削除されるようです。複数の項目を続けて挿入する必要があるため、コメント行を使用できません。期待どおりにdiv$('#thumbdest').html(data);内のすべてを置き換えます。thumbdest

以下は、使用するときに取得する必要があるHTMLですprepend

<tr>
    <td>
        <a href="#modal" class="thumbnail tableimg"  role="button" class="btn" data-toggle="modal">           
            <img src="assets/img/huvudbilder/thumbs/img.png" class="tableimg" alt="">
        </a>
    </td>
    <td>Test1</td>
    <td>Test2</td>                
</tr>

これは私が実際に使用することから得られるものですprepend

<a href="#modal" class="thumbnail tableimg"  role="button" class="btn" data-toggle="modal">
    <img src="assets/img/huvudbilder/thumbs/img.png" class="tableimg" alt="">
</a>
Test1
Test2

これにより、テーブル全体が台無しになります。すべてのテーブル要素が消えます、それはなぜですか?

編集:alert(data)プリペンドの後にテストするとき、私は以下を取得します:

<tr>
    <td>
        <a href="#editImage_1222866094" class="thumbnail tableimg"  role="button" class="btn" data-toggle="modal">

            <img src="assets/img/huvudbilder/thumbs/favicon32.png" class="tableimg" alt="">
        </a>
    </td>
    <td>Test1</td>
    <td>Test2</td>                     
</tr>

の構造#thumbdest

        <table class="table table-hover">
          <p>&nbsp;</p>
            <thead>
                <tr>
                    <th></th>
                    <th>Namn</th>
                    <th>Tid</th>
                </tr>
            </thead>
            <tbody id="thumbdest"></tbody>
            <tbody>
                <?php getImageList($conn); ?>
            </tbody>
        </table>
4

1 に答える 1

0
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Script Test</title>
    <script src="jquery.min.js"></script>
    <script>

    // example

        $(function() {
            $('.button').click(function(e) {
                e.preventDefault();
                var $mytext = "<a href=\"#\">Test</a>";
                    $("#key").prepend($mytext);

                });
        });

    </script>
</head>
<body>

    <a href="" class="button">Test</a>
    <div id="key"></div>

</body>
</html>

ここでprepend()は、タグを削除するのではなく、想定どおりに機能しています。

あなたがする必要がある<tbody>のは、内部を配置して<div id="thumbdest">から、次のスクリプト行をから$('#thumbdest').prepend(data);に変更することです$('#thumbdest tbody').prepend(data);

ソース: http: //forum.jquery.com/topic/prepend-stripping-out-html-table-tags

于 2012-11-14T08:38:12.993 に答える