【问题标题】:JQuery Mobile hide <input> does not hide the surrounding <div> of the <input> fieldJQuery Mobile hide <input> 不隐藏 <input> 字段周围的 <div>
【发布时间】:2017-03-13 10:12:53
【问题描述】:

当我在 JQuery Mobile 中使用 hide() 函数隐藏 &lt;input&gt; 标记时,它也隐藏了 &lt;input&gt;,它不会隐藏 JQuery Mobile 已实现的 &lt;input&gt; 周围的 &lt;div&gt;

我当然可以在之后处理 DOM 以使其消失,但我正在寻找更好的解决方案。

就是这样,你会注意到&lt;input type="text" name="lastname" id="lastname"&gt; 周围的div 仍然在屏幕上(你可以运行它a W3C editor like here):

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
  <h1>Text Inputs</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form method="post" action="/action_page_post.php">
      <div class="ui-field-contain">
        <label for="firstname">First name:</label>
        <input type="text" name="firstname" id="firstname">
        <label for="lastname">Last name:</label>
        <input type="text" name="lastname" id="lastname">
        <script type="text/javascript">
                    $(document).ready(
                        $('body').find('[id="lastname"]').hide()
                    );
        </script>           

      </div>
      <input type="submit" data-inline="true" value="Submit">
    </form>
  </div>
</div>

</body>
</html>

我尝试在parent() 上应用hide() 函数,但它确实将父级视为我要隐藏的&lt;div&gt; 尚未出现在屏幕上,而是隐藏整个表单特定领域的:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
  <h1>Text Inputs</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form method="post" action="/action_page_post.php">
      <div class="ui-field-contain">
        <label for="firstname">First name:</label>
        <input type="text" name="firstname" id="firstname">
        <label for="lastname">Last name:</label>
        <input type="text" name="lastname" id="lastname">
        <script type="text/javascript">
                    $(document).ready(
                        $('body').find('[id="lastname"]').parent().hide()
                    );
        </script>           

      </div>
      <input type="submit" data-inline="true" value="Submit">
    </form>
  </div>
</div>

</body>
</html>

【问题讨论】:

    标签: javascript jquery html css jquery-mobile


    【解决方案1】:

    在您尝试访问父 div 时,它没有被绘制。一种方法是使用 JavaScript setTimeout() 方法隐藏该 div 元素:

    setTimeout(function() {
      $('#lastname').parent().hide();
    }, 1);
    <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    
    <div data-role="page">
      <div data-role="header">
      <h1>Text Inputs</h1>
      </div>
    
      <div data-role="main" class="ui-content">
        <form method="post" action="/action_page_post.php">
          <div class="ui-field-contain">
            <label for="firstname">First name:</label>
            <input type="text" name="firstname" id="firstname">
            <label for="lastname">Last name:</label>
            <input type="text" name="lastname" id="lastname">
          </div>
          <input type="submit" data-inline="true" value="Submit">
        </form>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2013-12-05
      • 2012-08-16
      • 2015-07-23
      相关资源
      最近更新 更多