2013-06-02 2 views
0
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <title>Javascript Create Div Element Dynamically</title> 

    <style type="text/css"> 
.ex 
{ 
width:200px; 
position: relative; 
background-color :#CCC; 
height:150px; 
padding:10px; 
margin:5px; 
left-margin:0px; 
float :left; 
} 
#newdiv 
{ 
    width:800px; 
    height:800px; 
border:1px solid #000;  
} 
.border 
{ 
border:1px solid #000; 
} 

    </style> 
<script> 

image.cc=1; 
function changeimage(image) 
{ 
if (image.cc==0) 
    { 
    image.cc=1; 
    $(image).attr('src', 'images/white_contact.png'); 
    } 
else if (image.cc==1) 
    { 
    image.cc=2; 
    $(image).attr('src', 'images/yellow_contact.png'); 
    } 
    else if (image.cc==2) 
    { 
    image.cc=3; 
    $(image).attr('src', 'images/green_contact.png'); 
    } 
    else 
    { 
    image.cc=0; 
    $(image).attr('src', 'images/red_contact.png'); 
    } 
} 
</script> 
<script type="text/javascript" language="javascript"> 
var i=0; 
    function createDiv() 
    { 


    if(i < 6) { 
    $('#newdiv').append('<div id="div"'+i+'" class="ex" style="text-align: left;"><img class="myimage" onclick="changeimage(this)" border="0" src="images/white_contact.png" width="60"/></div>'); 

    $('#newdiv').find('#div'+i).append('<table border="0"><tr>'+ 
    '<td>Name:</td>'+ 
    '<td><input type="text"></td>'+ 
    '</tr><tr>'+ 
    '<td>Title:</td>'+ 
    '<td><input type="text"></td>'+ 
    '</tr>'+ 
    '<tr>'+ 
    '<td>Contact:</td>'+ 
    '<td><input type="text"></td>'+ 
    '</tr>'+ 
'</table>'); 

     } 

    i++; 
    $(".ex").draggable({containment:'parent',cursor:'pointer',opacity:0.6}); 
$(".ex").droppable(); 

    } 





    </script> 
</head> 
<body> 

    <p align="left"> 
     <b>Click this button to create div element dynamically:</b> 
     <input id="btn1" type="button" value="create div" onClick="createDiv();" /> 

     <div id = "newdiv"> 

</div> 
    </p> 

</body> 
</html> 

왜 텍스트 상자를 div에 추가하려고했는데 결과가 나타나지 않는 것 같습니까? 지금 어떻게해야합니까? 누구나 저에게 뭐가 잘못 됐는지 말해 줄 수 있습니까? .................................. 나는 단지 이미지 만 보았습니다. "ex"div의 배경색을 지정합니다.왜 텍스트 상자가 나타나지 않았습니까

$("#btn1").click(function(){ 
    // Your script to add the form 
}); 

내가이 jsFiddle에 넣어 한 예를 참조하십시오 :

+0

팁을 볼 수 있습니다. 그들은 쉽게 유지 관리 할 수 ​​있습니다. 마찬가지로를 [이] (http://jsfiddle.net/R6PmH/) – Kiruse

답변

1

그냥 DIV 사이에 큰 따옴표를 제거하고 '+ 내가 + 변경 "div"'+i+'""div'+i+'"

최종 코드

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <title>Javascript Create Div Element Dynamically</title> 

    <style type="text/css"> 
.ex 
{ 
width:200px; 
position: relative; 
background-color :#CCC; 
height:150px; 
padding:10px; 
margin:5px; 
left-margin:0px; 
float :left; 
} 
#newdiv 
{ 
    width:800px; 
    height:800px; 
border:1px solid #000;  
} 
.border 
{ 
border:1px solid #000; 
} 

    </style> 
<script> 

image.cc=1; 
function changeimage(image) 
{ 
if (image.cc==0) 
    { 
    image.cc=1; 
    $(image).attr('src', 'images/white_contact.png'); 
    } 
else if (image.cc==1) 
    { 
    image.cc=2; 
    $(image).attr('src', 'images/yellow_contact.png'); 
    } 
    else if (image.cc==2) 
    { 
    image.cc=3; 
    $(image).attr('src', 'images/green_contact.png'); 
    } 
    else 
    { 
    image.cc=0; 
    $(image).attr('src', 'images/red_contact.png'); 
    } 
} 
</script> 
<script type="text/javascript" language="javascript"> 
var i=0; 
    function createDiv() 
    { 


    if(i < 6) { 
    $('#newdiv').append('<div id="div'+i+'" class="ex" style="text-align: left;"><img class="myimage" onclick="changeimage(this)" border="0" src="images/white_contact.png" width="60"/></div>'); 

    $('#newdiv').find('#div'+i).append('<table border="0"><tr>'+ 
    '<td>Name:</td>'+ 
    '<td><input type="text"></td>'+ 
    '</tr><tr>'+ 
    '<td>Title:</td>'+ 
    '<td><input type="text"></td>'+ 
    '</tr>'+ 
    '<tr>'+ 
    '<td>Contact:</td>'+ 
    '<td><input type="text"></td>'+ 
    '</tr>'+ 
'</table>'); 

     } 

    i++; 
    $(".ex").draggable({containment:'parent',cursor:'pointer',opacity:0.6}); 
$(".ex").droppable(); 

    } 





    </script> 
</head> 
<body> 

    <p align="left"> 
     <b>Click this button to create div element dynamically:</b> 
     <input id="btn1" type="button" value="create div" onClick="createDiv();" /> 

     <div id = "newdiv"> 

</div> 
    </p> 

</body> 
</html> 

PS. 온 클릭 또는 바인딩 클릭 기능을 사용하는 것은 똑같습니다.

+0

여전히 작동하지 않습니다. – user2399158

+0

위의 업데이트 된 코드를 사용해보십시오. 제대로 작동합니다. – Dru

+0

회색 색상 div 내에서 텍스트 상자를 유지하는 방법 – user2399158

0

대신 onClick 사용을 사용하는 http://jsfiddle.net/DUU5v/2/

+0

이렇게하면 문제가 해결되지 않을 것입니다. http://stackoverflow.com/users/2399158/user2399158 div0, div1 등 요소로 div를 만들고 현재 코드가 동일한 ID "div"가 포함 된 모든 divs – Dru

0

당신은 내 대신 긴 DOM 문자열의 jQuery로 새로운 사업부와 아이를 구축하는 것 중 하나가 "추가,이 http://jsbin.com/uwiric/1/edit

$('#newdiv').append('<div id="div' + i + '" class="ex" style="text-align: left;"><img class="myimage" onclick="changeimage(this)" border="0" src="images/white_contact.png" width="60"/></div>'); 
           ^You have one extra " here 
관련 문제