简体   繁体   中英

How do I dynamically change div's colors after being created by jquery?

I attempted at first to perform this task with using jQuery's .css() but that failed. I have tried a couple of ways and they all failed. I would prefer to learn how to perform this function using jquery's library. jQuery UI is used for draggable and resizable UI.My

My current code:

<div id="p1"></div>

<link rel="stylesheet" href="script\jquery-ui-1.10.3.smoothness\css\smoothness\jquery-ui-1.10.3.custom.min.css" />
<script src="script/jquery-1.10.1.min.js"></script>
<script src="script/jquery-ui-1.10.3/ui/jquery-ui.js"></script>

<link rel="stylesheet" type="text/css" href="style/style.css">
<script type="text/javascript">
counter = 0;
$(function() {        
    $(document).on("click",'#addNewCo',function(e){
        e.preventDefault();
        var newCo = $('<div class="note" id="note'+counter+'" onclick="changecolor("note'+counter+'","EB0955")">note</div>').draggable({ scroll: true, scrollSpeed: 100 }).resizable();
        $('#area').append(newCo);
    });
});
function changecolor(id,color) {
    document.getElementById(id).style.backgroundColor = '#'+color;
}
</script>
<a id="addNewCo" href="javascript:void(0);"><button>New Note</button></a>
<div id="area"></div>

Don't use inline JavaScript for your new element, use .on() to delegate as you did in the other part of your code. Try:

$(function () {
    $(document).on("click", '#addNewCo', function (e) {
        e.preventDefault();
        var newCo = $('<div class="note" id="note' + counter + '")">note</div>').draggable({
            scroll: true,
            scrollSpeed: 100
        }).resizable();
        $('#area').append(newCo);
        counter++;
    });
    $(document).on("click", '.note', function () {
        changecolor($(this).attr('id'), 'EB0955');
    });
});

jsFiddle example

You also forgot to increment your counter. You can also use jQuery for your second function:

function changecolor(id, color) {
    $('#'+id).css('backgroundColor', '#' + color);
}

you can do

$(function() {        
$(document).on("click",'#addNewCo',function(e){
    e.preventDefault();
    var newCo = $('<div class="note">note</div>').draggable({ scroll: true, scrollSpeed: 100 }).resizable();
    $('#area').append(newCo);
});
$("#area").on("click",".note",function(){
     $(this).css("background","#EB0955");
});
});    

http://jsfiddle.net/8qXP3/

Here's another solution: http://jsfiddle.net/zbPaU/1/

This solution calls a binding function when you create the element.

$(function() {   
counter = 0;
$('#addNewCo').click(function(){
    var newCo = $('<div class="note" id="note'+counter+'" >note</div>');
    $('#area').append(newCo);
    nbind('#note'+counter);
    counter += 1;
    return false;
});
function changecolor(obj, color) {
    var colour = "#"+color;
    obj.css("background", colour);
}
function nbind(arg) {
    $(arg).bind('click', function() {
        changecolor($(this), "EB0955");
    });   
}
});

try this, maybe it can help:

HTML:

<a id="addNewCo" href="javascript:void(0);"><button>New Note</button></a>
<div id="area"></div>

JS:

counter = 0;
$('#addNewCo').click(function(e) {
    e.preventDefault();
    var newCo = $('<div />', {
                    class: 'note',
                    id: 'note' + counter,
                    click: function() {
                        $(this).css('background', '#EB0955');
                    }
                });
    newCo.draggable({ scroll: true, scrollSpeed: 100 }).resizable();

    $('#area').append(newCo);
});

CSS: (I use it to know when the note created, because I don't have your style)

.note {
    background: yellow;
    width: 50px;
    height: 50px;
}

This's fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM