简体   繁体   English

使用jQuery / javascript从HTML删除id属性

[英]Removing id attribute from HTML using jQuery/javascript

I have a mainWrap inside which content is populated dynamically.(It is somewhat like below) 我有一个mainWrap,里面的内容是动态填充的(如下所示)

          <div id="mainWrap">
                <div style="z-index: 1001; height: 407px; width: 328px; top: 150px; left: 601.5px;" id="com-TMRJDOR2KOET3X6JPV6XGU0FB7RGJ926" class="textBox contentBox">
                   <div style="" class="editable"><p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, </p>
                   </div>
                </div>

                <div style="z-index: 1002; height: 616px; width: 288px; top: 29px; left: 3.5px;" id="com-UPWTMKZ7OUTN8KG2JEK47LNPN5JO261H" class="textBox contentBox">
                   <div style="" class="editable"><p>
                ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
                  </div>
               </div>            
          </div>

The immediate children of mainWrap have an id attached to them.I want to remove the ids before I save it to the database.Now I have tried something like this mainWrap的直接子代都mainWrap一个ID。我想在将ID保存到数据库之前删除ID 。现在我已经尝试过类似的操作

        var getContent = $('#mainWrap');  
        var finalContent =getContent.clone().find('*').removeAttr('id');// 1            
        var finalContent=getContent.clone().children().removeAttr('id');//2     
        alert(finalContent.html());

In both the cases I get the output as follows: 在这两种情况下,我的输出如下:

        <div style="" class="editable"><p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, </p>
        </div>

The outer div is removed and instead of two I get only 1 div. 外部div被删除,而不是两个,我只有1 div。 But When I tried it out independently it works with the 2nd one. 但是当我独立尝试时,它可以与第二个一起使用。 Here's the fiddle 是小提琴

How can I get it right.I am not sure why its failing in the application and working independently! 我怎么才能正确呢?我不确定为什么它在应用程序中失败并且无法独立工作!
Thank you for the time 谢谢你的时间

Try be more specific in parents selection . 尝试更具体地选择父母。

Try this: 尝试这个:

$(document).ready(function () {
    var getContent = $('#mainWrap');
    var finalContent = $('div.textBox', getContent).clone();

    finalContent.each(function () {
        $(this).removeAttr('id');
    });
    console.log(finalContent[0]);
});

See it here http://jsbin.com/ewosix/1/ 在这里查看http://jsbin.com/ewosix/1/

 $('button').click(function () {
     var newContent = $(finalContent).first().wrap('<div class="addId" />');
     newContent = $(newContent).parents( /*You can add specific selector for parents like div.textBox or leave it emty for all parents */ ).each(function (index, value) {
         $(this).attr('id', index);
     });
     alert($(newContent).html());
 });

我不确定我是否理解正确,但是要删除#mainWrap div的直接子代的#mainWrap ,可以使用以下代码:

$('#mainWrap > *').removeAttr('id');

Use each 使用each

You can find useful help each jquery 您可以在每个jquery中找到有用的帮助

finalContent.each(function()
{
    $(this).removeAttr('id')
})

Because finalContent is object with more than one dom. 因为finalContent是具有多个dom的对象。

here is the fiddle. 是小提琴。 I think this is what you are looking for 我想这就是你要找的

Here is the js code: 这是js代码:

$(function(){
  $('button').click(function(){
     $("#wrap").children('div').each(function(){
        $(this).removeAttr('id');
     });
   alert($("#wrap").html());
  });
});

Maybe this is the solution for your problem: 也许这是您的问题的解决方案:

var mainWrap = $('#mainWrap').clone();
mainWrap.children().removeAttr('id');

alert(mainWrap.html()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM