简体   繁体   English

使用JavaScript注入HTML

[英]Using JavaScript to inject HTML

i have this in my view: 我认为:

 <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>

when i click on the reply button my text area pops up, i typed in some news and click on the post button. 当我单击“回复”按钮时,我的文本区域弹出,我输入一些新闻,然后单击“发布”按钮。 I need to display what i just typed underneath the grid_12 div. 我需要显示我在grid_12 div下面键入的内容。 the textarea must then be hidden again until i click on reply again 然后必须再次隐藏文本区域,直到我再次单击答复为止

I am struggeling to make this work. 我正在努力使这项工作。 can someone help please? 有人可以帮忙吗?

Add some div to display value and use jQuery in MVC3 you can use jQuery out of the box. 添加一些div以显示值并在MVC3中使用jQuery,您可以直接使用jQuery。 When you use $("#id") you select item by id, much like getElementById but it is wrapped set of jQuery with which you can do much more than selecting it normally. 当您使用$(“#id”)时,您可以按ID选择项目,就像getElementById一样,但它是jQuery的封装集,使用它,您可以比通常选择它做更多的事情。

new div for displaing 新div用于置换

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

some jQuery code to put in onClick: 一些放在onClick中的jQuery代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM