繁体   English   中英

javascript for循环相关问题

[英]javascript for loop related question

我尝试使用for循环作为全部列表的替代

function init(){
    new dEdit($('editBox'));
    new dEdit($('editBox2'));
    new dEdit($('editBox3'));
   }

被...取代

function init(){
           for(var i = 0; i < 1000; i++){
                                new dEdit($('editBox'+i));
           }

}  

但似乎对我不起作用。 如何纠正?

谢谢

以下是不带“ for循环”的完整工作代码

<?xml version="1.0" encoding="ISO-8859-1"?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title> New Document </title>
  <meta name="title" content="" />
  <meta name="author" content="0xs.cn" />
  <meta name="subject" content="" />
  <meta name="language" content="zh-cn" />
  <meta name="keywords" content="" />
  <style type="text/css" >
   /* default css rule */
   body { font: 12px "Verdana"; }
  </style>
  <script type="text/javascript" >
   // shortcut
   function $(s){
    return typeof s == 'object'?s:document.getElementById(s);
   }
   var dEdit = function(el){
    var me = this;
    this.save = function (txt){
     el.innerHTML = txt;
    };

    this.edit = function (e){
     var e = e || event;
     var target = e.target || e.srcElement;
     if(target.tagName.toLowerCase() == 'input'){
      return;
     }
     var ipt = document.createElement('input');
     ipt.value = target.innerHTML;
     ipt.onkeydown = function(){
      if((arguments[0]||event).keyCode==13){
       me.save(this.value);
      }
     };
     ipt.onblur = function(){
      me.save(this.value);
     };
     target.innerHTML = '';
     target.appendChild(ipt);
     ipt.focus();
    };

    el.onclick = this.edit;
   }; 
   function init(){
    new dEdit($('editBox'));
    new dEdit($('editBox2'));
    new dEdit($('editBox3'));
   }
   window.onload = init;
  </script>
 </head>

  <body>
   <span id="editBox">This is sample text.</span> <br/><br/>
   <span id="editBox2">This is sample text 222.</span> <br/><br/>
   <span id="editBox3">This is sample text 333.</span>
  </body>

 </html>

将范围标签上的ID更改为“ editBox0”,“ editBox1”和“ editBox2”。 另外,您在20到30分钟前就发布了这个完全相同的问题,然后有人也给了您正确答案。

您需要为ID选择器添加#前缀。 将您的代码更改为:

function init(){
           for(var i = 0; i < 1000; i++){
                                new dEdit($('#editBox'+i));
           }

}

除其他问题外,例如在空jQuery对象上调用dEdit(...)时会发生什么...

您需要使用#前缀来选择ID:

$('#editBox2')

您的循环从0到1000,并且editBoxN不是有效的ID选择器,因此您以

new dEdit($('editBox0'));
new dEdit($('editBox1'));
new dEdit($('editBox2'));
...

更改第一个editBox ID,循环变量,并将哈希添加到jquery选择器以匹配ID

function init(){
       for(var i = 1; i < 1000; i++){
           new dEdit($('#editBox'+i));
       }
 }  

问题是初始化因为第一个值引发了期望,因为'editBox0'不存在。 要解决此问题,您可以将每个循环迭代包装在try / catch中。

例如

function init(){
    for(var i = 0; i < 1000; i++){
        try {
            new dEdit($('editBox'+i));
        } catch (e) {}
    }
}

这样,如果未定义id,脚本仍将运行。 另外,如果要在循环中分配<span id="editBox0"><span id="editBox1"> ,则应将<span id="editBox">更改为。

暂无
暂无

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

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