2012-01-06 13 views

risposta

15

Non utilizzare stringhe di codice HTML per tutto!

$(this).append(
    $('<input>', { 
     type: 'text', 
     val: $('#div1').text() 
    }) 
); 
+0

Grazie. Probabilmente il modo migliore – jQuerybeast

+0

@minitech hai qualche risorsa su non usare stringhe html? mi piacerebbe leggere su di esso. Grazie – jQuerybeast

+0

@jQuerybeast: non in mano. Potrei scrivere un post sul blog ora però :) Il motivo è che non devi preoccuparti di eseguire l'escape e tale, o errori di battitura che si manifestano solo in fase di esecuzione. – Ryan

2
$(this).append('<input type="text" value='+ $('#div1').html()+ '>'); 
+0

btw ... i div non hanno un valore 'val()' –

+0

@JosephMarikle yeap. non l'ho notato –

1
$(this).append('<input type="text" value=' + $('#div1').val() + '>'); 

non dimenticate di concatonate con +

Inoltre, questo presuppone $ (questo) è un oggetto reale.

+2

btw ... i div non hanno un valore 'val()' –

+1

tuttavia a un input può essere assegnato un id di div1. questo potrebbe essere ancora corretto dal momento che non ha fornito html. –

+1

lol molto vero ma un po '... improbabile. –

0

<div id="parent-dv"> 
    <lable for="posting">POST</lable> 
    <textarea style="width:400px; height:auto;"></textarea> 
    <input type="Submit" id="inputsubmit"> 
    <button id="clear">Clear</button> 
</div> 

$(document).ready(function(){ 
 
$('#inputsubmit').on('click',function(event) 
 
\t { 
 
var $inpute2xt = $("#parent-dv").find('textarea').val(); 
 
$('#parent-dv').append("<p></p>").addClass("newp").append($inpute2xt); 
 
\t \t \t 
 
\t } 
 
\t \t); 
 
\t 
 
\t } 
 
\t); 
 
\t 
 
\t
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <style> 
 
    </style> 
 
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
</head> 
 
<body> 
 
    
 
<div id="parent-dv"> 
 
\t <lable for="posting">POST</lable> 
 
\t <textarea style="width:400px; height:auto;"></textarea> 
 
\t <input type="Submit" id="inputsubmit"> 
 
\t <button id="clear">Clear</button> 
 
\t <p></p> \t 
 
\t </div> 
 
    
 

 
    
 
</body> 
 
</html>

Problemi correlati