Getting an empty JQuery object

前端 未结 4 522
北海茫月
北海茫月 2020-11-30 04:20

In the following code I set up a change handler on a select box to show and hide some follow up questions based on the value of the selection.

Further, for some valu

相关标签:
4条回答
  • 2020-11-30 04:49

    My advice is don't do it that way. There are a lot easier ways of doing this. Consider:

    <select id="select" name="select">
      <option value="msg_1">Message 1</option>
      <option value="msg_2">Message 1</option>
      <option value="msg_3">Message 1</option>
    </select>
    
    <div class="msg_1 msg_3">
      ...
    </div>
    
    <div class="msg_1">
      ...
    </div>
    
    <div class="msg_2">
      ...
    </div>
    
    $(function() {
      $("#select").change(function() {
        var val = $(this).val();
        $("div." + val").show();
        $("div:not(." + val + ")").hide();
      });
    });
    

    Much easier. Basically give classes to indicate what to show and hide and then there is no tracking required. An alternative is:

    $(function() {
      $("#select").change(function() {
        var val = $(this).val();
        $("div").each(function() {
          if ($(this).hasClass(val)) {
            $(this).show();
          } else {
            $(this).hide();
          }
        });
      });
    });
    
    0 讨论(0)
  • 2020-11-30 04:52

    try this

     var new = $([]);
    

    I ain't a good programmer but it gives u empty object :))

    0 讨论(0)
  • 2020-11-30 04:53

    This creates an empty jQuery-object:

    $([])
    

    Update: In newer versions of jQuery (1.4+), you can use:

    $()
    
    0 讨论(0)
  • 2020-11-30 05:12
    $();
    

    Returning an Empty Set

    As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set (with a .length property of 0). In previous versions of jQuery, this would return a set containing the document node.

    Source: api.jquery.com

    0 讨论(0)
提交回复
热议问题