简体   繁体   English

使用jQuery IE 7“停止运行此脚本”错误

[英]IE 7 “stop running this script” error using jQuery

I can't seem to find what is causing this error in IE 7 works in Chrome. 我无法在Chrome浏览器中找到IE 7中导致此错误的原因。 When loading my page in internet explorer it pops up the error "stop running this script message". 在Internet Explorer中加载我的页面时,会弹出错误“停止运行此脚本消息”。 Any ideas? 有任何想法吗?

 $(document).ready(function() {
    var icons = {
    header: "ui-icon-circle-plus",
    headerSelected: "ui-icon-circle-minus"
    };  

   $('.ui-accordion').accordion({
   active: false,
   collapsible: true,
   autoHeight: false,
   icons: icons
   });

   $("a").click(function (event) {
    event.stopPropagation();
   });

   $('.requirementCheckBox').click(function() {
     getReq();
   });
 });


function getReq() {

  var componentList;
  var selected = $(":checkbox:checked");

  if(selected.length ==0){
    $('#requirements_table_wrapper').remove();
  }

  else {
    $.each(selected , function(i, n){

    if(i == 0){
      componentList = n.value;
    }
    else{
      componentList += ',' + n.value;
    }
 });


$.getJSON("addRequirements/GetRequirements/?componentList=" + componentList, function    (data) {

  $('#requirements_table_wrapper').remove();

    var reqString = '<table id="requirements_table"><thead><tr><th>Requirement ID</th><th>Requirements</th><th>Reference</th></tr></thead><tbody>';

    for (var i = 0; i < data.length; i++) {
      reqString += '<tr><td>'+ data[i].reqID  + '</td><td>' + data[i].reqText + '</td>' + '<td>' + data[i].reqReference + '</td></tr>';
    }

    reqString += '</tbody></table>';

    $("#requirementsDiv").append(reqString);

    $("#requirements_table").dataTable({
        "bJQueryUI": true,
        "bPaginate": false,
        "bRetrieve": true,
    "oLanguage": {"sSearch" : "Filter Requirements:"}
    });

  });
}

} }

I don't immediately spot any infinite loops but maybe I have been staring at it too long. 我不会立即发现任何无限循环,但也许我一直盯着它看太长时间。

**UPDATE The problems seems to be with the accordion, once it is removed IE loads the page normally. **更新问题似乎与手风琴有关,一旦删除,IE正常加载页面。

Use $("#requirementsDiv").html(reqString); 使用$("#requirementsDiv").html(reqString); instead of the .append() method. 而不是.append()方法。

$elem.html(string) is equivalent to elem.innerHTML = string . $elem.html(string)相当于elem.innerHTML = string
$elem.append(string) first converts the string to a DOM element, then appends the elements to the HTML using the DOM .appendChild() methods. $elem.append(string)首先将字符串转换为DOM元素,然后使用DOM .appendChild()方法将元素附加到HTML。

Since you're executing the code at page load, it's very likely that the contents of the div is empty. 由于您在页面加载时执行代码,因此div的内容很可能是空的。 If the div is not empty, but doesn't contain event-handlers and such, also use .html() : 如果div不为空,但不包含事件处理程序等,也可以使用.html()

var $elem = $("#requirementsDiv");
$elem.html($elem.html() + reqString);

One immediate place where I see room for improvement is where you are concatinating your reqString using + and +=. 我看到改进空间的一个直接位置是使用+和+ =来汇总你的reqString。 Don't do that, instead push each piece onto an array and then join the array on "", and then append to your doc. 不要这样做,而是将每个部分推送到一个数组上,然后将数组加入“”,然后附加到您的文档。

var reqString = ['<table id="requirements_table"><thead><tr><th>Requirement ID</th><th>Requirements</th><th>Reference</th></tr></thead><tbody>'];

    for (var i = 0; i < data.length; i++) {
      reqString.push('<tr><td>', data[i].reqID, '</td><td>', data[i].reqText,'</td>','<td>', data[i].reqReference, '</td></tr>');
    }

    reqString.push('</tbody></table>');
$("#requirementsDiv").append(reqString.join(""));

Another place to look would be your use of $.each. 另一个要看的地方是你使用$ .each。 Try changing it to a regular for loop as the $.each isn't always as efficient as for. 尝试将其更改为常规for循环,因为$ .each并不总是如此高效。

As a side note you have an error in your script where you add '})' to close out getReq. 作为旁注,你的脚本中有一个错误,你在其中添加'})'以关闭getReq。

It does not have to be an infinite loop, but some part of your script that takes too long. 它不一定是无限循环,但是脚本的某些部分需要太长时间。

Try to remove parts of the script until the error goes away, then you will have found the part you need to optimize. 尝试删除部分脚本,直到错误消失,然后您将找到需要优化的部分。

You have two loops that should be considered first: 您应该首先考虑两个循环:

$.each(selected , function(i, n){

and

for (var i = 0; i < data.length; i++) {

The second can be slightly optimized by using an array, if the data array is really large: 如果数据数组非常大,则可以使用数组稍微优化第二个:

var reqArray = ['<table id="requirements_table"><thead><tr><th>Requirement ID</th><th>Requirements</th><th>Reference</th></tr></thead><tbody>'];

for (var i = 0; i < data.length; i++) {
  var element = data[i]
  reqString.push('<tr><td>'+ element.reqID  + '</td><td>' + element.reqText + '</td>' + '<td>' + element.reqReference + '</td></tr>');
}

var regString = regArray.join('') + '</tbody></table>';

But I don't think that this will remove the problem. 但我不认为这会消除这个问题。 Still worth a try. 还是值得一试。

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

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