最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery .clone() - Stack Overflow

programmeradmin3浏览0评论

I'm currently trying to clone with the following code:

var position = $(this).position();
var ptop = position.top;
var pleft = position.left;
$(this).click(function() {
                $(this).clone().css({
                    top: ptop,
                    left: pleft,
                    opacity: '0.55'
                })
}).appendTo(this);

I need the element to clone at the exact position than the brother element. Thats why I have:

    var position = $(this).position();
    var ptop = position.top;
    var pleft = position.left;

For getting the position. But I also what the clone to have a lighter opacity.

I'm currently trying to clone with the following code:

var position = $(this).position();
var ptop = position.top;
var pleft = position.left;
$(this).click(function() {
                $(this).clone().css({
                    top: ptop,
                    left: pleft,
                    opacity: '0.55'
                })
}).appendTo(this);

I need the element to clone at the exact position than the brother element. Thats why I have:

    var position = $(this).position();
    var ptop = position.top;
    var pleft = position.left;

For getting the position. But I also what the clone to have a lighter opacity.

Share Improve this question edited Nov 23, 2011 at 19:37 Adam Rackis 83.4k57 gold badges278 silver badges400 bronze badges asked Nov 23, 2011 at 19:36 Shawn31313Shawn31313 6,0724 gold badges41 silver badges85 bronze badges 5
  • Please be more specific. Your question is? The close is not working? What is happening wrong? – user800014 Commented Nov 23, 2011 at 19:39
  • So what is the problem exactly? – Ben Commented Nov 23, 2011 at 19:39
  • I assume it isn't work or you wouldn't be posting it here, but please be specific, what error do you get or what exactly is the problem? Maybe an example of the problem on jsFiddle would help you get a good answer. – Matt Cofer Commented Nov 23, 2011 at 19:40
  • Sorry, Nothing is happening, the clone element is not being appended. – Shawn31313 Commented Nov 23, 2011 at 19:42
  • This is referring to: $('#drag') – Shawn31313 Commented Nov 23, 2011 at 19:42
Add a ment  | 

1 Answer 1

Reset to default 6

You've got your parentheses messed up. Your call to "appendTo()" is being applied after the establishment of the "click" handler.

$(this).click(function() {
                $(this).clone().css({
                    top: ptop,
                    left: pleft,
                    opacity: '0.55'
                }).appendTo(this);
});

You need it inside the "click" handler, and it needs to be called on the ".clone()" return value.

发布评论

评论列表(0)

  1. 暂无评论