How to use element of a certain parent
I have one element, when dragged to another开发者_JAVA技巧 place that element is cloned and i have two elements with same id and etc, but the parent is different, how can i set some style or whatever to the cloned element, but not affecting the older one, from witch i cloned. And again every class and id is the same, just different parent .
You need to prefix any attributes with the parent's id. For example instead of using
.header {
color:red;
}
You would use
#someParentId .header {
color:red;
}
or with jQuery
$("#someParentId .header").css({color:"red"});
And just as a side note, try not to clone nodes with ids. An id should be unique.
$('#id_of_different_parent').find('.cloned_class')
When you say different parent, I will assume it's different either by its ID or by its class.
Let's say that both the original and cloned element have class class1
, for the sake of the examples below.
1. If parents are different by their IDs: id1 and id2
To differentiate the styling of the original and cloned element you would have to include in your CSS:
#id1 .class1
{
background-color: red;
}
#id2 .class1
{
background-color: green;
}
which would result in the original and cloned element having red and green background colors, depending under which parent (id1
or id2
) each resides.
2. If parents are different by their classes: class11 and class12
To differentiate the styling of the original and cloned element you would have to include in your CSS:
.class11 .class1
{
background-color: red;
}
.class12 .class1
{
background-color: green;
}
which would again result in the original and cloned element having red and green background colors, depending under which parent (class11
or class12
) each resides.
Don't use so many id's. Try to not clone any element with an id. If you do, it is neutered until added to the document, so remove the old id as soon as you make the clone.
精彩评论