使用JavaScript注入HTML

时间:2012-12-04 13:46:10

标签: javascript asp.net-mvc-3 html5

我认为这是:

 <div class="grid_12">
<div class="box tabbed news" id="box1">
   <div class="header">
    <h3>
       <%: Model.News.Title %> 
    </h3>
  </div>
  <div class="content" id="newsItem">
    <% Html.RenderPartial("NewsItem", Model); %>
  </div> 
  <div class="clear"></div>
 </div>
 </div> <!-- End of .grid_12 -->

 <div class="clear"></div>
 <table id="table2">

 </table>
 <div class="grid_12">
 <div class="box tabbed news" id="box2">
   <div class="content" id="testdiv">
     <table id="table">
       <tr>
         <td>
           <a href="javascript:ShowReplyTextArea();" class="button">Reply</a>
         </td>
         <td>
           <a href="javascript:ReplyPost(<%: Model.News.NewsId %>);" class="button" id="post_button">Post</a>
         </td>
       </tr>
     </table>
    </div>
  </div>
</div>
<div class="clear"></div>
</asp:Content>

<asp:Content ID="Content5" ContentPlaceHolderID="ScriptPlaceHolder" runat="server">
<script type="text/javascript">
 function ShowReplyTextArea() {
   div = document.getElementById('testdiv')
   textArea = document.createElement("textarea");
   textArea.setAttribute('rows', 20);
   textArea.setAttribute('cols', 149);
   textArea.setAttribute('id', "textarea");

   div.appendChild(textArea);
 }

 function ReplyPost(newsId) {
   var message = $("#textarea").text();
   if (message != null)
     $("#post_button").show();
   var jqxhr = $.getJSON("<%= Url.Action("ReplyPost", "Home", new { area = "News" }) %>?newsId=" + newsId + "&message=" + message, function (data) {
   });

   divtext = document.getElementById('table2');
   divtext.setAttribute('text', message);
 }
</script>

当我点击回复按钮时弹出我的文本区域,我输入了一些新闻并点击了帖子按钮。我需要显示我在grid_12 div下面输入的内容。然后必须再次隐藏textarea,直到我再次点击回复

我很努力地做这项工作。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

添加一些div来显示值,并在MVC3中使用jQuery,您可以使用开箱即用的jQuery。 当你使用$(“#id”)时,你可以选择id的项目,就像getElementById一样,但它是jQuery的包装集合,你可以做更多的事情而不是正常选择它。

用于替代的新div

<div id="displayArea"></div>

一些jQuery代码放在onClick:

$("#textArea").hide();
$("#displayArea").innerText = $("#textArea").val();