English 中文(简体)
Java印功能
原标题:Javascript function anchor onclick syntax

I had following script for comment system working nicely until I put parameters into function call. Now instead of the function executing, it just reloads the page and bumps me up to the top. (Before, I had it not bumping and nicely inserting box.) Can anyone see error in following. Note, I gather that # is not preferred way to call functions from link, but other ways seemed rather complicated for this simple function call. Many thanks.

请注意,本页是描述“comments.php”的文字,而id和专题论者是 in。

<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>
最佳回答

Have the function return false at the end to prevent the browsers default behavior of following the anchor.

Here is a working Fiddle: http://jsfiddle.net/VhYd8/

问题回答

替换<代码>#, 贴标签上javascript: 避免(0)

<a href="javascript:void(0);" onclick="showReplyBox(44,142, comments.php );return false">Reply</a>
<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>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签