2012-12-06 6 views
2

나는 여러 div에서 데이터를 게시하는이 자바 스크립트가 있지만 나는 linebreaks 계속 유지할 수없는 것.줄 바꿈을 유지하지 Jquery 게시물

필자가 찾은 내용에 .text()를 사용한다는 사실과 관련이 있다고 생각합니다. 아마 줄 바꿈을 유지하지 않을 수도 있습니다. 나는 .html()로 시도했지만 어떤 이유로 든 div에 새 줄에 작성된 모든 텍스트를 div로 작성합니다.

코드는 다음과 같습니다 :

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#actor_result").load('xml/actortoxml.php?owner=<?php echo $_SESSION['username'] . "&id=" . $_GET['id']; ?>'); 
    $('#save_editable').click(function() { 
     var name = $(document).find('#actorname').text(); 
     var current = $(document).find('#currenthealth').text(); 
     var max = $(document).find('#maxhealth').text(); 
     var effects = $(document).find('#actoreffects').text(); 
     var notes = $(document).find('#actornotes').text(); 

     $.ajax({ 
      url: 'actor_profile.php', 
      type: 'POST', 
      data: { 
       update: 'true', 
       actorid: <?php echo $_GET['id']; ?>, 
       actorname: name, currenthealth: current, maxhealth: max, actoreffects: effects, actornotes: notes 
      }, 
      success : function() { 
      // gets called when ajax request completes successfully 
      $("#actor_result").hide().load('xml/actortoxml.php?owner=<?php echo $_SESSION['username'] . "&id=" . $_GET['id']; ?>').fadeIn(500);  
      }, 
      error : function(err) { 
       // in case of error 
       console.log(err); 
       alert('error'); 
      } 
     }); 
    }); 
}); 
</script> 

편집, 이것은 xml 파일에서 HTML 생성됩니다

<div id="actor_result" style="display: block;"> 
    <!--?xml version="1.0"?--> 

    <div class="row-fluid row span6 offset3"> 
     <div class="media well well-small"> 
      <a class="pull-left" href="#"><img class= 
      "media-object img-circle circle128" src= 
      "images/avatar/actor/Davius.png"></a> 

      <div class="media-body page-header actor-profile"> 
       <div class="editable_name" contenteditable="true" id= 
       "actorname"> 
        <h4 class="media-heading">Davius</h4> 
       </div><small><strong>awarnhag's minion</strong></small> 
      </div><strong>Health:</strong> 

      <div class="div_inline editable_hp" contenteditable="true" id= 
      "currenthealth"> 
       17 
      </div>/ 

      <div class="div_inline editable_hp" contenteditable="true" id= 
      "maxhealth"> 
       20 
      </div>hp 
     </div> 

     <h5><span class="label">Effects</span></h5> 

     <div class="editable well well-small" contenteditable="true" id= 
     "actoreffects"> 
      Mumblecore bushwick sed, nulla street art dolore delectus wolf 
      american apparel artisan sriracha. Laboris seitan hoodie, 
      freegan brooklyn letterpress adipisicing chambray mixtape id 
      tofu organic butcher small batch. Art party carles readymade 
      messenger bag williamsburg. Irony placeat sustainable, high 
      life cillum yr sed vinyl pork belly messenger bag williamsburg 
      VHS. Occaecat lo-fi readymade gluten-free 3 wolf moon. Ad tofu 
      twee, blog nulla mumblecore gentrify brooklyn odio cliche 
      selvage put a bird on it pork belly chillwave deserunt. Ea 
      assumenda chillwave, keytar velit tumblr pour-over enim VHS 
      mcsweeney's blog.aaaa 
     </div> 

     <h5><span class="label">Notes</span></h5> 

     <div class="editable well well-small" contenteditable="true" id= 
     "actornotes"></div> 
    </div> 
</div> 
+1

어리석은 생각을하지만, 어쩌면 시도 CSS :'* {공백 : 사전}'http://stackoverflow.com/a/656648/483371 – andytuba

+0

당 당신은 XML을 게시 할 수 있습니까? – trebuchet

+0

xml은 xsl에 의해 html로 변환됩니다. – Marty

답변

0

위의 코드 조각에서 벌어지고 정확하게 결정하기 어렵다.

하면, <BR> -tags에 " \ n", 또는 그 반대로 변환 시도 되세요 중 하나는 (POST) 데이터를 전송하기 전이나 데이터 ($ .load)를받은 후? " #actor_result"(div, textarea, input 등)의 태그 유형은 무엇입니까?

+0

저는 자바 스크립트에 익숙하지 않아 도움이되지 않으면 새로운 방법을 시도하기가 어렵습니다. #actor_result는 xsl에서 html로 변환 된 xml이있는 div입니다. 예를 들어, #actornotes는 id가 "actor_result"인 div 내에서 편집 가능한 div입니다. – Marty

1

innerHTML을 가져 와서 수동으로 모든 div와 br를 대체 할 수 있습니다.

$("#id").html() 
     .replace(/<(br|p|div)[^>]*>/g, "\n") 
     .replace(/<\/?\w+[^>]*>/g, ""); 

http://jsfiddle.net/tarabyte/GsECc/4/

+0

정확히 내가 뭘 찾고 있었는지, 정말 고마워! = D – Marty

+0

그러나 그것은 완벽한 해결책이 아닌 것 같습니다. 나는 단락의 일부분을 몇 줄을 밀어 넣으려고했는데 절약했을 때 아무것도하지 않았고 내가 편집하기 전의 상태로 돌아갔습니다. – Marty

+0

검색 패턴의 일반 버전을 추가했습니다. –

관련 문제