【问题标题】:How can I assign Id attribute to all the children of a contenteditable div on keyup?如何在 keyup 上将 Id 属性分配给 contenteditable div 的所有子项?
【发布时间】:2015-08-08 17:05:39
【问题描述】:

我尝试了以下方法:

HTML:

<div contenteditable="true" id="editable"></div>

JS:

$('#editable').keyup(function() {
    addID();
});

function addID()
{
    $('#editable *').each(function() {

        var t = GenerateID();

        $(this).attr('id','id-' + t);

    });
}

function GenerateID() 
{
    var str = 'abcdefghijklmnopqrstuvwxyz0123456789';

    var alphabet = '', 
        genID = '';

    while(genID.length < 5)
    {
        alphabet = str.charAt(Math.floor(Math.random() * str.length)); 
        genID += alphabet;
    }

    return genID;
}

但在每次按键时,它都会不断更改 ID。

如何在键入时为所有元素设置一次id,并在整个div 中保持其唯一性?

JSFiddle

【问题讨论】:

  • 检查是否设置了?如果是,就不要再设置了?
  • @x4rf41 喜欢哪种方式?

标签: javascript jquery contenteditable unique-id


【解决方案1】:

最后更新: 现在我检查了你小提琴中的代码,我确信它可以工作。检查唯一性可能会变成一个函数,但我会把它留给你:

$('#editable').on( 'keyup', addID );


var count = 0;  // this will absolutely ensure that ID will be unique

function addID(){  

    var previousIDs = [];

    $('#editable *').each(function() {

        count++;
        var thisID = $(this).attr( 'id' );

        // let's check if we have duplicates:
        var index = 0, len = previousIDs.length, isDuplicate = false;

        for( index = 0; index < len; index++ ){
            if ( thisID === previousIDs[index] ) { 
                isDuplicate = true; 
                break;
            }
        }


        // now change the ID if needed:
        if (  isDuplicate    ||    ! thisID  ){

            var t = GenerateID();
            var newID = 'id-' + t + '-' + count;

            $(this).attr('id', newID);
            previousIDs.push( newID );

        }else{
            previousIDs.push( thisID );
        }

    });
}

工作Fiddle

【讨论】:

  • 那也是重复id值!
  • 所以让我直截了当地说:您想为#editable 的所有孩子提供一个唯一的ID,并且您想在用户第一次点击某个键时执行此操作。之后,停止更改 ID。我的理解正确吗?
  • 停止更改已设置一次的ID。根据您的建议,它只分配一次 ID,但是当您按 Enter 时,contenteditable div 会创建另一个 div 并为其分配与前一个分配相同的值。正如文档所说,id 的值在整个文档中应该是唯一的。
  • 我没有看到任何创建新 div 的代码!它在哪里?
  • 签入浏览器的检查器!
【解决方案2】:

试试这个:

$('#editable').keyup(addID);
function addID() {
    $('#editable *').each(function () {
        var t = GenerateID();
        var elem = $(this);
        var attr = elem.attr('id');
        if (!attr) {
            elem.attr('id', 'id-' + t);
        }
    });
}
/**
 * @return {string}
 */
function GenerateID() {
    var str = 'abcdefghijklmnopqrstuvwxyz0123456789';
    var alphabet = '',
            genID = '';
    while (genID.length < 5) {
        alphabet = str.charAt(Math.floor(Math.random() * str.length));
        genID += alphabet;
    }
    return genID;
}

还要考虑你的随机字符串生成器可能会再次生成相同的字符串。

【讨论】:

  • 我试过了,但方式不同。它只设置一次,但会在下一个连续元素中重复 ID 值。
  • 我猜你的问题不完整。 $('#editable *') 这会返回什么?
  • 它返回#editable div中的每个元素
  • 我看不到你提供的任何小提琴
  • 检查控制台。这个$(this).prop("tagName") + ' = ' + t 在每次按键时记录以下内容:DIV = j5728
【解决方案3】:

用以下代码替换您的代码:

$('#editable *').each(function() {

        if(!$(this).hasClass("idgenerated")){
            console.log( $(this).attr('id') );

            var t = GenerateID();

            $(this).attr('id','id-' + t);
            $(this).addClass("idgenerated");
            console.log($(this).prop("tagName") + ' = ' + t);
        }
});

Working fiddle

【讨论】:

  • 设置一一元素的if后不起作用。
  • 它仍然分配相同的 ID:&lt;div contenteditable="true" id="editable"&gt;div1&lt;div id="id-6x525" class="idgenerated"&gt;div2&lt;/div&gt;&lt;div id="id-6x525" class="idgenerated"&gt;div3&lt;/div&gt;&lt;div id="id-6x525" class="idgenerated"&gt;&lt;br id="id-v52ik" class="idgenerated"&gt;&lt;/div&gt;&lt;/div&gt;
猜你喜欢
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 2012-10-11
  • 1970-01-01
相关资源
最近更新 更多