2014-06-06 76 views
-1
<html> 

<head> 
<title>Edit it</title> 


<script type="text/javascript" src="scripts/base/jquery.min.js"></script> 
<script type="text/javascript" src="scripts/base/library_live.js"></script> 


<style> 
canvas{ 
    border: 1px solid black; 
} 
</style> 

</head> 

<body> 


<textarea id="123"> </textarea> 


<script> 
$("#123").keyup(function(){ 
    $("#user-code").html($(this).val()); 
    $("#user-code").text($(this).val()); // If you want html code to be escaped 
}); 
</script> 
<div id="user-code" style="display:none;"> 



</div> 

<canvas 
width="400" 
height="500" 
class="codehs-editor-canvas"></canvas> 

<script> 
var g = new CodeHSGraphics({ 
    canvas: $('.codehs-editor-canvas') 
}); 
// run test # here. 
g.runCode($("#user-code").text()); 

</script> 

</body> 
</html> 

好吧,我希望用戶鍵入文本區域,然後在div用戶代碼中顯示代碼。謝謝:)我已經嘗試了很多東西,但我無法得到它的工作謝謝! 更新 從建議仍然不工作 我還應該指出,div將顯示應用程序或某種形式,使用自定義庫。當我把代碼放在文本區域時,它仍然不會在畫布上顯示代碼。從文本區域框中獲取文本,並將其放入div

另一種方法是將文本區域保存爲文本文檔,然後在刷新頁面後使用該文本文檔作爲div id。

回答

5

Live demo

爲了使文本顯示在div標籤。您應該刪除style="display:none"和使用jQuery代碼:

$("#123").keyup(function(){ 
    $("#user-code").html($(this).val()); 
    // $("#user-code").text($(this).val()); // If you want html code to be escaped 
}); 

或者這個jQuery將處理隱藏和顯示你:

$("#123").keyup(function(){ 
    if($(this).val() == ""){ 
     $('#user-code').css('display','none'); 
    }else{ 
     $('#user-code').css('display','block'); 
    } 
    $("#user-code").html($(this).val()); 
    // $("#user-code").text($(this).val()); // If you want html code to be escaped 
}); 
+2

打我吧! :) 好答案! – lindsay

+0

@Amir不應該首先顯示塊到#用戶代碼? – aandis

+0

是的,我現在正在更新我的答案 – CMPS

0
$("#user-code").css({"display" : "block"}); 
$("#123").keyup(function(){ 
    $("#user-code").html($(this).val()); 
    // $("#user-code").text($(this).val()); // If you want html   code to be escaped 
}); 
相關問題