2017-04-07 72 views
1

我想了解以下JFiddle例如轉換成一個單一的HTML網頁:http://jsfiddle.net/zqa511e9/使用JFiddle創建網頁

我究竟做錯了什麼?樣式和JavaScript代碼都沒有反映在表單中。任何幫助將不勝感激。

<!DOCTYPE html> 
<html> 
<head> 

<title>Expandable Text Area</title> 

<style> 
textarea { 
width: 200px; 
height:15px; 
line-height:15px; 
min-width: 200px; 
max-width: 300px; 
transition: width 0.25s; 
resize:none; 
overflow:hidden; 
} 
</style> 

<script> 
$('textarea').on('keydown', function(e){ 
if(e.which == 13) {e.preventDefault();} 
}).on('input', function(){ 
$(this).height(1); 
var totalHeight = $(this).prop('scrollHeight') - 
parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom')); 
$(this).height(totalHeight); 
}); 
</script> 

</head> 

<body> 

<textarea placeholder="Autosize" data-autosize-input='{ "space": 40 }'> 
</textarea> 

</body> 
</html> 
+0

你需要外部資源:jQuery和jquery.autosize.input.js –

+0

究竟如何添加到網頁? – Sharon99

回答

0

對於一個你使用jQuery,但沒有鏈接到您的HTML jQuery。 Jfiddle讓它自動應用我認爲。

搶jQuery的CDN,並將其放置在頭標記是這樣的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
+0

謝謝Stefan。 Pratiyush的代碼有效。 我不得不像上面提到的那樣添加jquery鏈接並修復javascript塊。 謝謝大家的幫助。 – Sharon99

0

你需要外部資源:jQuery和jquery.autosize.input.js

在HTML放之間的這種代碼頭標籤

<head> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/autosize.js/3.0.20/autosize.min.js"></script> 
</head> 
1

更新後的代碼。

你需要做兩件事情。 1. SImport Jquery庫。 2.在javascript塊中寫入加載。

嘗試這個

<title>Expandable Text Area</title> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
</head> 
<style> 
textarea { 
width: 200px; 
height:15px; 
line-height:15px; 
min-width: 200px; 
max-width: 300px; 
transition: width 0.25s; 
resize:none; 
overflow:hidden; 
} 
</style> 

<script> 
$(document).ready(function() { 
    $('textarea').on('keydown', function(e){ 
    if(e.which == 13) {e.preventDefault();} 
    }).on('input', function(){ 
    $(this).height(1); 
    var totalHeight = $(this).prop('scrollHeight') - 
    parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom')); 
    $(this).height(totalHeight); 
    }); 
}); 
</script> 

</head> 

<body> 

<textarea placeholder="Autosize" data-autosize-input='{ "space": 40 }'> 
</textarea> 

</body> 
</html> 
+0

如何在JavaScript塊中編寫onload? – Sharon99

+0

我已經在例子中做了。嘗試在當地運行它。 –

+0

我複製了你的代碼,但它仍然不能正常工作 – Sharon99