0

これにはjQueryを使用していますが、現在は使用できません(理由はありません)。テーブルの一部を選択して印刷する必要があります。

私はこのコードを使用します:

function Printon(this)
{
    newWin= window.open('','','toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=800, height=1000, left=10, top=25');
    newWin.document.write('<table>' + document.getElementsByName(this).innerHTML + '</table>');
    newWin.document.close();
    newWin.focus();
    newWin.print();
    newWin.close();
    return false;
}

次に、名前のパラメーターを使用して関数を呼び出すと、<tr>次のドキュメントを取得します。

12-11-12

undefined

私のHTMLは正しいと確信していますが、それでも私のHTMLは次のとおりです。

<tr name="Print_1">
    <td>dhr Kees-jan ckc Von fleppenstein</td>
    <td>aquaduct straat 66<br>0606 OP ORK</td>
    <td>WDB-1352303696</td>
    <td>2822.00</td>
    <td><input type="button" value="Print this" onclick="Printon('Print_1')"></td>
    <td> &nbsp;</td>
</tr>
<tr name="Print_1">
    <td>-</td>
    <td> &nbsp;</td>
    <td> &nbsp;</td>
    <td>46.00</td>
    <td>2</td>
    <td>Actiesalade herfst</td>
</tr>
<tr name="Print_1">
    <td>-</td>
    <td> &nbsp;</td>
    <td> &nbsp;</td>
    <td>46.00</td>
    <td>2</td>
    <td>Actiesalade herfst</td>
</tr>

@Muthu Kumaran のインスピレーションで、私はこれを使用します:

function Printon(dit)
{
    var arr = new Array(); 
    arr = document.getElementsByName(dit); 
    var tabel = '';
    for(var i = 0; i < arr.length; i++){
         tabel += document.getElementsByName(dit)[i].outerHTML;
    }

    newWin= window.open('','','toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=800, height=1000, left=10, top=25');
    newWin.document.write('<table border=\"2\" cellpadding=\"2\" cellspacing=\"0\">' + tabel + '</table>');
    newWin.document.close();
    newWin.focus();
    newWin.print();
    newWin.close();
    return false;
}

修正されて動作するようになりました!

4

2 に答える 2

1

this関数の引数として使用しないでください。名前のような適切な変数を付けます。またdocument.getElementsByName、要素の配列を返すため、を使用して値を取得する必要がありますdocument.getElementsByName(name)[0]

これを試して、

function Printon(name)
{
    newWin= window.open('','','toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=800, height=1000, left=10, top=25');
    newWin.document.write('<table>' + document.getElementsByName(name)[0].innerHTML + '</table>');
    newWin.document.close();
    newWin.focus();
    newWin.print();
    newWin.close();
    return false;
}

実際のデモ: http://jsfiddle.net/muthkum/RxNKn/1/

于 2012-11-12T09:26:26.857 に答える
0

文字列'Print_1'を渡し、関数でこれを使用しています。「this」には「Print_1」は含まれません。

これの代わりに変数を使用することをお勧めします。関数を次のように変更します。

function Printon(var tableName)
                {
                    newWin= window.open('','','toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=800, height=1000, left=10, top=25');
                    newWin.document.write('<table>' + document.getElementsByName(tableName).innerHTML + '</table>');
                    newWin.document.close();
                    newWin.focus();
                    newWin.print();
                    newWin.close();
                    return false;
                }

お役に立てれば!!!

于 2012-11-12T09:24:35.360 に答える