JQuery删除元素使页面变得无响应并且浏览器挂起

时间:2017-01-05 03:25:09

标签: javascript jquery html

我尝试循环使用JQuery删除我的html中的元素,但是当删除元素的函数运行时,我的浏览器挂起并且我必须终止该过程,因为页面变得无响应,这里是JQuery代码:

function removeElement(){
            var i =0;
            for(i=1;i<=5;i++){
              if ($('#attribute-name'+i).length > 0){
                  $('#attribute-name'+i).remove();
                  $('#attribute-custom'+i).remove(); 
              } else{
                break;
              }
            }
          }

我的Html:

<table>
  <tr>
    <td id='attribute-name0'></td>
    <td id='attribute-custom0'></td>
    <td id='attribute-name1'></td>
    <td id='attribute-custom1'></td>
    <td id='attribute-name2'></td>
    <td id='attribute-custom2'></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

尝试使用这个衬垫:

$("td:regex(id, attribute-name\d)").remove()
$("td:regex(id, attribute-custom\d)").remove()

首先,我们使用正则表达式选择器选择所有元素,然后调用remove方法删除所有节点。