1

在将参数放入函数调用之前,我的注释系统运行良好。现在不是执行函数,它只是重新加载页面并将我撞到顶部。(之前,我让它没有碰撞并且很好地插入盒子。)任何人都可以看到以下错误。请注意,我认为 # 不是从链接调用函数的首选方式,但对于这个简单的函数调用,其他方式似乎相当复杂。非常感谢。

注意thispage 是一个文本字符串,即“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 回答