我正在開發一個社交網站,如facebook在php中。我有一個頁面,用於顯示帶有兩個鏈接的用戶的消息,即收件箱中每條消息的右下角的reply
和delete
。我的問題是,當我點擊鏈接reply
,這兩個鏈接的文本應更改爲post
和back
。我的HTML代碼是這樣的:使用javascript更改鏈接文本點擊鏈接
<div class="msgutil">
<a href="" id="reply" >reply</a>
<a href="delete_message.php?msg_id=<?php echo $row_msg_id;?>" id="delete">delete</a>//dont check this
</div><!--end msgutil-->
我的JavaScript代碼是這樣的:
$(document).ready(function()
{
//action to be done if link name is reply which is present below the message box
$("#reply").click(function(e)
{
//action to be done if link name has changed from "reply" to "post" which is present below the message box
if(document.getElementById('reply').innerHTML=="reply")
{
document.getElementById('reply').innerHTML="Post";
document.getElementById('delete').innerHTML="Back";
var idstr = document.getElementById('sender_id').value;
$('<br/><form id="msgreply" name="msgreply" action="post_messages.php" method="post"> <label for="txt_subject"><strong>Subject:</strong></label><br/><input type="text" id="txt_subject" name="txt_subject"/><br/><label for="txt_message"><strong>Message:</strong></label><br/><input type="hidden" id="to_id" name="to_id" value="'+idstr+'"/><textarea name="txt_message" id="replyfield"></textarea></form>').insertAfter("#temp");
e.stopPropagation();
e.preventDefault();
}
else if(document.getElementById('reply').innerHTML=="Post")
{
var message = document.getElementById('msgreply');
message.submit();
e.stopPropagation();
e.preventDefault();
}
});
//action to be done if link name is delete which is present below the message box
$("#delete").click(function(e)
{
//action to be done if link name has changed from "reply" to "post" which is present below the message box
if(document.getElementById('delete').innerHTML=="Back")
{
$("#msgreply").remove();
document.getElementById('reply').innerHTML="reply";
document.getElementById('delete').innerHTML="delete";
e.stopPropagation();
e.preventDefault();
}
});
});
它對第一條消息正常工作。如果收件箱中有多條消息,則在單擊除第一條消息之外的任何消息的答覆按鈕時,不會執行任何操作。
,請告訴我 – Techy
我讀你關於ň意見後做的原因或者確定你是否使用jQuery。祝您好運 – Alexander