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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么