JQuery Mobile hide <input />不会隐藏<input />字段的周围<div>

时间:2017-03-13 10:12:54

标签: javascript jquery html css jquery-mobile

当我在JQuery Mobile中隐藏<input>hide()函数时,它隐藏<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> 上屏幕,并隐藏整个表单而不是特定的字段:

   public class ViewPart1 extends ViewPart {

    public ViewPart1() {
        // TODO Auto-generated constructor stub
    }

    private ResourceManager resourceManager = new LocalResourceManager(
            JFaceResources.getResources());

    @Override
    public void createPartControl(Composite parent) {
        // re-use an existing image
        final Image image = FieldDecorationRegistry.getDefault()
                .getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION)
                .getImage();
        TableViewer tblView = new TableViewer(parent);

        final Table table = tblView.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableViewerColumn fn = new TableViewerColumn(tblView, SWT.BORDER, 0);
        fn.getColumn().setWidth(150);
        fn.getColumn().setText("Firstname");
        fn.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                Person p = (Person) element;
                return p.getFirstName();
            }

            @Override
            public Image getImage(Object element) {
                return image;
            }
        });
        // fn.setEditingSupport(new EditColumn(tblView));

        fn = new TableViewerColumn(tblView, SWT.BORDER, 1);
        fn.getColumn().setWidth(150);
        fn.getColumn().setText("Last name");

        fn.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                Person p = (Person) element;
                return p.getLastNAme();
            }

            @Override
            public Image getImage(Object element) {

                return image;
            }
        });
        // fn.setEditingSupport(new EditColumn(tblView));

        tblView.setContentProvider(new QContentProvider());
        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person("a", "b"));
        list.add(new Person("C", "D"));
        tblView.setInput(list);
        tblView.refresh();

    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

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