javascript - JQuery Mobile 隐藏 &lt;input&gt; 不会隐藏 &lt;input&gt; 字段周围的 <div>

标签 javascript jquery html css jquery-mobile

当我隐藏 <input> 时带有 hide() 的标签JQuery Mobile 中的函数,它还隐藏了 <input>它不会隐藏 <div>周边<input>这已由 JQuery Mobile 实现。

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

在这种情况下,您会注意到 <input type="text" name="lastname" id="lastname"> 周围的 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>

我尝试应用 hide() parent() 上的函数但它确实将父级视为 <div>我想要隐藏的内容尚未出现在屏幕上,并隐藏整个表单而不是特定字段:

<!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>

最佳答案

当您尝试访问父 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>

关于javascript - JQuery Mobile 隐藏 &lt;input&gt; 不会隐藏 &lt;input&gt; 字段周围的 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42761072/

相关文章:

javascript - 如何根据类别附加正确的详细信息?

javascript - NG-模型不适用于属性指令

javascript - 将鼠标悬停在图像效果上

php - jquery 不适用于每个 href 元素

javascript - 如何在 HTML 文件中编码图像数据?

javascript - php脚本执行倒计时

javascript - 具有绝对定位图像的响应式页面

javascript - 将元素 ID 添加到 js if 语句

html - CSS 的优先顺序是什么?

html - CSS 样式文本未按预期工作