dongyongan9941 2018-01-15 08:07
浏览 66
已采纳

尝试获取div并通过jquery克隆它的问题

I'm pretty new to web programming so, making a logic for a web-page, I made a mistake and now I'm in trouble.

I named some divs like "Dv_1.2.1.3" (without knowing the problems linked to using the dot) and I have issues trying to clone (via jquery called by button) some of these.

The button id contains the id of the div I want to clone, so, my logic is:
1) extract the id of the div;
2) get the div and clone it (giving a new id).

I'm stuck with getting the div because of the dots in the id.

The below code is what I've done so far:

$('.CloneDiv').click(function () {
    var SplittedId = (this.id).split('_');
    if (SplittedId[0]=='Clone'){
        alert('SplittedId 1 =' + SplittedId[1]);

        //Modify id to use it to find the div to clone
        var UsableId = SplittedId[1].replace(/\./g, '\\\\.');
        alert('UsableId =' + UsableId);
        //Count existing elements
        var ClonedNum = $('#' + 'Dv_' + UsableId + '_').length;
        ClonedNum++;
        var OrigElem = $('#' + 'Dv_' + UsableId).length;
        alert('OrigElem =' + OrigElem); //THIS IS 0
        //NO ELEMENTS FOUND BUT THE ELEMENT EXISTS
        //Clone the element and give new id
        var ClonedElem = $('#' + 'Dv_' + UsableId).clone().attr('id', function( i, val ) {
                                                                return val + '_' + ClonedNum;
                                                                });
        ClonedElem.find("input").val("");

        if (ClonedNum > 1){
            ClonedNum--;
            var AnteId = '#' + 'Dv_' + UsableId + '_' + ClonedNum;
            alert(AnteId);
            $(AnteId).after(ClonedElem);
        }else{
            var AnteId = '#' + 'Dv_' + UsableId;
            alert('AnteId = ' + AnteId);
            $(AnteId).after(ClonedElem);
        };


    }else if(SplittedId[0]=='Del'){
        alert(SplittedId[0]);
        alert('Del');
    }else{
        //error
    };
});
  • 写回答

1条回答 默认 最新

  • douciwang6819 2018-01-15 08:48
    关注

    Might these help: developer.mozilla.org/en-US/docs/Web/API/CSS/escape , Polyfill: github.com/mathiasbynens/CSS.escape/blob/master/css.escape.j‌​s

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现