2012-05-06 92 views
1

我有很好的評論系統工作的腳本,直到我把參數放入函數調用。現在,而不是執行的功能,它只是重新加載頁面,讓我碰到頂端。 (在此之前,我沒有碰撞並很好地插入盒子。)任何人都可以在下面看到錯誤。請注意,我認爲#不是從鏈接調用函數的首選方式,但其他方式對於此簡單函數調用來說似乎相當複雜。非常感謝。Javascript函數錨onclick語法

注意這頁是一個文本字符串,即「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

在結尾處函數返回false防止以下錨的瀏覽器的默認行爲代替錨標記上的#

這裏是一個工作小提琴:http://jsfiddle.net/VhYd8/

+0

你的代碼和javascript:void下面的組合似乎這樣做。但是,直到我從小提琴剪切和粘貼代碼才能起作用。我的語法中有些奇怪。 – user1260310

+0

@ user1260310好吧,很高興它幫助你! –

4

javascript:void(0)

<a href="javascript:void(0);" onclick="showReplyBox(44,142,'comments.php');return false">Reply</a> 
+0

THX。我採取了很好的措施。 – user1260310

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>