单击页面上任何其他位置时隐藏列表项

时间:2017-09-06 06:13:12

标签: javascript jquery html css

我创建了一个搜索用户功能,但只要用户点击页面上的任何其他位置就会出现问题,除非用户删除他/她所写的所有单词以显示匹配的结果,否则列表不会消失好的事情我尝试了很多,但它仍然没有工作div是可见的无论在哪里我们点击页面的所有。

new Thread(new Runnable() {
                    public void run() {
                        final JSONObject jsonObject = uploadImage(strImagePath);
                        if (jsonObject != null) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    Log.d(TAG, "Response : " + jsonObject.toString());

                                }
                            });
                        }
                    }
                }).start();

public JSONObject uploadImage(String profileFileUri) {
        try {
            final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
            RequestBody r = null;
            RequestBody k = null;
            String extension = null;
            String ext = null;

            String userID = "1";

            if (profileFileUri != null) {
                File sourceFile = new File(profileFileUri);

                Log.d(TAG, "File...::::" + sourceFile + " : " + sourceFile.exists());

                final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");
                final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
                final MediaType MEDIA_TYPE_GIF = MediaType.parse("image/gif");

                if (profileFileUri.contains(".jpg")) {
                    k = RequestBody.create(MEDIA_TYPE_JPG, sourceFile);
                    ext = ".jpg";
                } else if (profileFileUri.contains(".jpeg")) {
                    ext = ".jpeg";
                    k = RequestBody.create(MEDIA_TYPE_JPEG, sourceFile);
                } else if (profileFileUri.contains(".png")) {
                    ext = ".png";
                    k = RequestBody.create(MEDIA_TYPE_PNG, sourceFile);
                } else if (profileFileUri.contains(".gif")) {
                    ext = ".gif";
                    k = RequestBody.create(MEDIA_TYPE_GIF, sourceFile);
                }
            }
            ArrayList<String> imgList = new ArrayList<>();
            MultipartBody requestBody;

            MultipartBody.Builder mRequestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM);
            mRequestBody.addFormDataPart("Test", Consts.token_auth);
            mRequestBody.addFormDataPart("proccess_id", "1");
            mRequestBody.addFormDataPart("img1", "IMG" + System.currentTimeMillis() + ext, k);
            mRequestBody.addFormDataPart("img2", "IMG" + System.currentTimeMillis() + ext, k);
            mRequestBody.addFormDataPart("img3", "IMG" + System.currentTimeMillis() + ext, k);
            mRequestBody.addFormDataPart("img4", "IMG" + System.currentTimeMillis() + ext, k);
            mRequestBody.addFormDataPart("img5", "IMG" + System.currentTimeMillis() + ext, k);
            mRequestBody.addFormDataPart("if_shop_act", "yes");

            for (MediaPojo pojo : aryMedialist) {

                mRequestBody.addFormDataPart("shop_act[]", pojo.getName().replace(" ", "_") + ".png", RequestBody.create(MEDIA_TYPE_PNG, new File(pojo.getPath())));
                imgList.add(pojo.getName());

                Log.v("ImageData", pojo.getName() + " - " + pojo.getPath());

            }


            RequestBody rb = mRequestBody.build();

            Log.v("Test", android.text.TextUtils.join("|", imgList));

            Log.d(TAG, "uploadImage param  : " + rb.toString());
            okhttp3.Request request = new okhttp3.Request.Builder()
                    .url("Your URL")
                    .post(rb)
                    .build();


            OkHttpClient client = new OkHttpClient();
            okhttp3.Response response = client.newCall(request).execute();

            String res = response.body().string();

            Log.d(TAG, "Response : " + res);
            return new JSONObject(res);

        } catch (UnknownHostException | UnsupportedEncodingException e) {


            Log.e(TAG, "Error: " + e.getLocalizedMessage());
            e.printStackTrace();
        } catch (Exception e) {


            Log.e(TAG, "Other Error: " + e.getLocalizedMessage());
            e.printStackTrace();
        }

        return null;
    }
$(document).ready(function(e) {
  var start = /@/ig;
  var word = /@(\w+)/ig;
  var $contentbox = $('#contentbox'),

    $display = $('#display');

  $contentbox.on('keyup', function(e) {
    var target = e.target;
    var $box = $(this),
      content = $box.text(),
      go = content.match(start),
      name = content.match(word);

    len = $.trim($contentbox.text()).length;

    if (!$(target).is('#display') && !$(target).parents().is('#display')) {
      $('#display').hide();
    }

    if (go && go.length) {
      $display.slideUp('show');
      if (name && name.length) {
        var html = '<ul class="tg_1"><li><p>test 1</p></li></ul><ul class="tg_1"><li><p>test 1</p></li></ul>';
        $display1.html(html).show();
      }
      console.log(go.length);
    }

    return false;
  });

  $(document).on("click", ".addname", function() {
    var username = $(this).attr('title');
    var old = $("#contentbox").html();
    var content = old.replace(word, "");
    $("#contentbox").html(content);
    var E = username;
    $("#contentbox").append(E);
    $("#display").hide();
    $("#contentbox").focus();
  });
});
.st_n_ar>form {
  position: relative;
}

#display_co,
#display {
  position: absolute;
  width: 98%;
  background-color: #fff;
  padding: 10px;
  left: 6px;
  top: 123px;
  display: none;
  z-index: 99;
}

.st_n_ar>form>#contentbox {
  width: 98%;
  height: 81px;
  margin: auto;
  background-color: #efefef;
  padding: 15px 12px;
  padding-bottom: 45px;
  overflow: auto;
  margin-bottom: 40px !important;
}

.st_n_ar>form>#sb_art {
  position: absolute;
  right: 6px;
  bottom: -35px;
  background-color: #0d1927;
  border: none;
  height: 35px;
  line-height: 35px;
  text-transform: uppercase;
  font-size: 12px;
  color: #fff;
  padding: 0 15px;
  transition: ease-in-out all .5s;
  -webkit-transition: ease-in-out all .5s;
  -moz-transiotion: ease-in-out all .5s;
  cursor: pointer;
}

ul.tg_1 {
  padding-left: 0;
  list-style-type: none;
  overflow: hidden;
  margin: 0;
}

.tg_1>li {
  margin: 5px auto;
}

Jquery Fiddle Demo

1 个答案:

答案 0 :(得分:1)

你正在寻找这个吗?

$(document).mouseup(function(e) {
    var container = $("#contentbox");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        $('#display').html('').hide(); // hide the result div block
        $("#contentbox").text(''); // clear the field
    }
});