Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

jquery - make resizable a dynamically add div

I have a div called content

<div id="content">
</div>

I have another div call instruments

<div id="instruments">

</div>

When I click on an instrument it has to added to a div called content. I have done this job. But I need to the moved div (instrument) is resizable. I have tried using jquery-ui.js but it failed. what am i going to do? the script is bellow

<script>
var steps_array = [];
$(".instruments").one("click", function(){
        $("#content").append($(this));
        $(this).css({"position":"relative", "top":"10px"});
        $(this).addClass('resizable');

    steps_array.push({
        id : $(this).attr('id'),
        height: $(this).height(),
        width: $(this).width(),
        positionX: $(this).position().left,
        positionY: $(this).position().top
    });

        $( this ).draggable({ 
            containment: "parent",
        drag: function() {
            $("#x-value").html($(this).position().top); 
            changePos ( $(this).attr('id'), $(this).position().left , $(this).position().top );
            console.log("Edited : "+$(this).attr('id'));

        }       
        });

$( ".resizable" ).resizable();    
    });


    $(".btn").click(function(){
        print_array_object(steps_array);
    });
        function print_array_object(array_name){
        $.each(array_name, function(index, val) {
            console.log(val.id);
            console.log(val.height);
            console.log(val.width);
            console.log(val.positionX);
            console.log(val.positionY);
        });
    }

    function changePos( id, newX , newY) {
       for (var i in steps_array) {
         if (steps_array[i].id == id) {
            steps_array[i].positionX = newX;
            steps_array[i].positionY = newY;
            break; //Stop this loop, we found it!
         }
       }
    }
    </script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a syntax error in your javascript where you have .one() instead of .on() and you are using id="instruments"in your html when it should be class="instruments"

Also make sure you are linking to:

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />

<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

var steps_array = [];
$(".instruments").on("click", function(){ //here you had ".one()" instead of .on()

	$("#content").append($(this));
	$(this).css({"position":"relative", "top":"10px"});
	$(this).addClass('resizable');

    steps_array.push({
        id : $(this).attr('id'),
        height: $(this).height(),
        width: $(this).width(),
        positionX: $(this).position().left,
        positionY: $(this).position().top
});

$( this ).draggable({ 
	containment: "parent",
	drag: function() {
	$("#x-value").html($(this).position().top); 
	changePos ( $(this).attr('id'), $(this).position().left , $(this).position().top );
	console.log("Edited : "+$(this).attr('id'));
	}  
 });

$( ".resizable" ).resizable();    
});


$(".btn").click(function(){
	print_array_object(steps_array);
});
function print_array_object(array_name){
	$.each(array_name, function(index, val) {
		console.log(val.id);
		console.log(val.height);
		console.log(val.width);
		console.log(val.positionX);
		console.log(val.positionY);
	});
}

function changePos(){
  //not sure what this does but added to avoid errors 
  }
#content{
   width:300px;
   height:300px;
  background-color:#cccccc;
  }

.instruments{
   width:50px;
   height:50px;
  background-color:#999999;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<div id="content">
</div>

<div class="instruments"> <!-- here you were using an id instead of a class -->

</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...