开发者

In YUI3 why does Y.one return null?

I've got a webpage that validates as XHTML 1.0 Strict. I'm using YUI3 and I'm using the seed-file-based instantiation. In several places in my javascript code, I'm doing something like:

YUI().use("node", function(Y){
  var node = Y.one("#my_element_id");
});

It works great, cross-platform, cross-browser, etc. in almost every case. However, I was testing yesterday, and I came across one time it didn't work. It made no sense to me, the element I was trying to grab was:

<form id="component_form" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
...
</form>

I know for sure it is well-formed markup, so that's not the issue. If I do:

YUI().use("node", function(Y){
  var node1 = Y.one("#component_form");
  var node2 = document.getElementById("component_form");
  var node3 = Y.one(document.getElementById("component_form"));
});

node1 is null, and node2 is the element I was looking for, and so is node3.

Anyone have a similar experience, or know if this is a YUI3 bug, or what?

Here is a full markup example:

<!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" lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
    <!-- metadata -->
    <title>Inventory Management System</title>      
    <script type="text/javascript" src="http://yui.yahooapis.com/3.1.1/build/yui/yui-min.js"></script>
    <script src="./util.js" type="text/javascript"></script>    
    <style type="text/css">
      td{
        vertical-align:text-top;
      }  

      legend{
        border: 2px #D4D0C8 groove;
        padding: 2px;       
        font-weight: bolder; 
      }

      fieldset{
        border: 2px #D4D0C8 groove;
        padding-bottom: 12px;              
        padding-left: 10px;
        padding-right: 10px;
      }

      label{
        font-weight: bold;
      }

      #err_container{
        color: red;
        display: none;
        visibility: hidden;
        margin: 10px;
      }

      #status_container{
        color: green;
        display: none;
        visibility: hidden;
        margin: 10px;
      }      
    </style>

  </head>
  <body>
    <div id="canvas">
      <form id="st_frm" method="post" action="" style="display:none; visibility: hidden;">        
        <fieldset style="border:none; margin:0; padding:0;">          
          <input type="hidden" name="state" id="st" value=""/>            
        </fieldset>
      </form>

      <div id="navbar">
              <a href="#" onclick="goToState(0); return false;">home</a> 
       | components |         <a href="#" onclick="goToState(2); return false;">products</a> 
            </div>


              <h1 id="main_h1">

            Update Component
                  </h1>
        <form id="component_form" method="post" action="/inventory/index.php">
          <fieldset>            
            <legend id="component_form_legend">Component Information</legend>
            <input type="hidden" id="component_form_id" name="id" value="8"/>
            <input type="hidden" name="state" value="1"/>
            <table>

              <tr>
                <td><label for="manufacturer_name_id">Manufacturer:</label></td>
                <td><input type="text" id="manufacturer_name_id" name="manufacturer_name" value="Vishay"/></td>
              </tr>
              <tr>
                <td><label for="manufacturer_part_number_id">Part Number:</label></td>
                <td><input type="text" id="manufacturer_part_number_id" name="manufacturer_part_number" value="1123114123"/></td>
              </tr>

              <tr>
                <td><label for="ct_id">Component Type:</label></td>
                <td>
                  <select id="ct_id" name="component_type">
                    <option value="0">New Type</option>                    
                                        <option value="5" >sfkd</option>
                                        <option value="6" >qwrqew</option>

                                        <option value="7"  selected="selected" >Resistor</option>

                  </select>
                  <input id="nct_id" type="text" name="new_component_type" size="10"  style="visibility:hidden; display: none;" />
                </td>
              </tr>
              <tr>
                <td><label for="description_id">Description:</label></td>
                <td>

                 开发者_StackOverflow中文版 <textarea id="description_id" name="description" rows="3" cols="25">limits the flow of current...</textarea>
                </td>
              </tr>
              <tr>
                <td>&nbsp;</td>                                  
                <td>
                                    <input id="component_form_submit_button" type="button" value="Update Component"/>
                  <span id="component_form_hide_when_new" >
                    <input id="component_form_delete_button" type="button" value="Delete Component"/>

                    <input id="component_form_new_button"    type="button" value="New Component"/>
                  </span>                  
                  <input id="component_form_delete" name="delete" type="hidden" value="0"/>
                </td>
              </tr>
              <tr>
                <td colspan="2">
                  <div id="error_container">&nbsp;</div>
                  <div id="status_container">&nbsp;</div>                  
                </td>

              </tr>
            </table>
          </fieldset>
        </form>

        <ul id="component_form_list">        
          <li>
            <a href="#" onclick="setNodeValue('component_form_id', 8); submitForm('component_form'); return false;">Vishay 1123114123</a>
          </li>

        </ul>        


    </div>

  </body>
</html>

EDIT IE 8 crashes on the var s = ... line because frm is null.

function submitForm(frmId){
  YUI().use("node", function(Y){
    var frm = Y.one("#" + frmId);    
    var s = typeof frm.submit;
    if(s === 'function'){
      frm.submit();
    }
  });    
}

but....

function submitForm(frmId){
  YUI().use("node", function(Y){
    var frm = Y.one(document.getElementById(frmId));    
    var s = typeof frm.submit;
    if(s === 'function'){
      frm.submit();
    }
  });    
}

works in both...


Did you miss one double quote?

<form id="component_form" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜