代码之家  ›  专栏  ›  技术社区  ›  Jorge Israel Peña

原型选择帮助

  •  0
  • Jorge Israel Peña  · 技术社区  · 15 年前

    嘿,伙计们,我想在我使用的网络应用程序上做一个小的添加。现在我正在检查页面上所有有类的复选框 .checkBox ( 如果需要区分/选择 )复选框是类的div的后代 .someClass 只是有很多女主角都上过这个课。我要选中属于类为的div的后代的框。 只有 躯体 .

    换言之:

    <!-- Check this box -->
    <div class="someClass"> [...] <input type="checkbox" class="checkBox" /></div>
    
    <!-- But not this one -->
    <div class="someClass otherClasses lolWut"> [...] <input type="checkbox" class="checkBox" /></div>
    

    记住,复选框不是直接子项,而是后代。

    谢谢,我非常感谢您的帮助:)

    1 回复  |  直到 13 年前
        1
  •  3
  •   No Surprises    15 年前

    干得好:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
        <title>Check the boxes</title>
        <script src="prototype.js" type="text/javascript"></script>
    
        <script type="text/javascript">
        function checkThem() {
          // get all the .someClass divs
          $$(".someClass").each(function(item) {
            // filter out the ones that have additional classes 
            if(item.className == 'someClass') {
              // get the .checkBox descendants
              item.select('.checkBox').each(function(checkbox) {
                // check them
                checkbox.checked = true;
              });
            }
          });   
        }
        </script>
    
    </head>
    
    <body>
    
      <!-- Check this box -->
      <div class="someClass">Check: <input type="checkbox" class="checkBox" /><input type="checkbox" class="checkBox" /></div>
    
      <!-- But not this one -->
      <div class="someClass otherClasses lolWut">Don't check: <input type="checkbox" class="checkBox" /></div>
    
      <!-- Check this box -->
      <div class="someClass">Check: <input type="checkbox" class="checkBox" /><input type="checkbox" class="checkBox" /></div>
    
      <br /><br />
    
      <a href="#" onclick="checkThem(); return false;">Check them.</a>
    
    </body>
    </html>