1

パラメータを関数呼び出しに入れるまで、コメントシステムの次のスクリプトがうまく機能していました。これで、関数を実行する代わりに、ページをリロードして一番上に移動します。(以前は、ぶつかったり、箱をうまく挿入したりしていませんでした。)フォロー中にエラーが表示されることはありますか。#はリンクから関数を呼び出すための好ましい方法ではないことに注意してください。しかし、この単純な関数呼び出しでは他の方法はかなり複雑に見えました。どうもありがとう。

このページはテキスト文字列、つまり「comments.php」であり、idとtopicidは整数であることに注意してください。

<script>
function showReplyBox(id,topicid,thispage) {
    var replybox = '<form action = "newcomment.php" method = "post"><input type="hidden" name="comid" value="';
replybox = replybox+ id + '">';
replybox = replybox + '<input type="hidden" name="topicid" value="';
replybox = replybox + topicid + '">';
replybox = replybox + '<input type="hidden" name="thispage" value="';
replybox = replybox + thispage + '">';
replybox = replybox + '<textarea autofocus placeholder="Reply to comment" id="replyarea" rows=1 cols=72></textarea><br><button>Reply</button></form>';
    var empty = ""; 

document.getElementById('replybox').innerHTML = replybox;
}
</script>
<body>
//link to call function
<a href="#" onclick="showReplyBox(44,142,'comments.php');return false">Reply</a

//box inserted here
<div id="replybox"></div>
</body>
4

3 に答える 3

4

#アンカータグのを次のように置き換えますjavascript:void(0)

<a href="javascript:void(0);" onclick="showReplyBox(44,142,'comments.php');return false">Reply</a>
于 2012-05-06T15:39:15.240 に答える
4

falseブラウザーがアンカーに従うというデフォルトの動作を防ぐために、関数を最後に返すようにします。

これが動作するフィドルです:http://jsfiddle.net/VhYd8/

于 2012-05-06T15:39:43.353 に答える
0
<body>
//link to call function
<a href="#" id="my_a" t_id=44 t_tid=142 t_php="comments.php">Reply</a>
<script>
    function showReplyBox(event) {
        event.preventDefault();
        var tar_a = e.target;
        var id = tar_a .getAttribute("t_id");
        var topicid = tar_a .getAttribute("t_tid");
        var thispage = tar_a .getAttribute("t_php");

        var replybox = '<form action = "newcomment.php" method = "post"><input type="hidden" name="comid" value="';
        replybox = replybox+ id + '">';
        replybox = replybox + '<input type="hidden" name="topicid" value="';
        replybox = replybox + topicid + '">';
        replybox = replybox + '<input type="hidden" name="thispage" value="';
        replybox = replybox + thispage + '">';
        replybox = replybox + '<textarea autofocus placeholder="Reply to comment" id="replyarea" rows=1 cols=72></textarea><br><button>Reply</button></form>';
        var empty = ""; 

        document.getElementById('replybox').innerHTML = replybox;
    }
    doucument.getElementById("my_a").addEventListener('click',showReplyBox,false);
</script>
//box inserted here
<div id="replyarea"></div>
</body>
于 2012-05-06T16:03:39.377 に答える