检查style =“ height:0px”

时间:2019-03-08 18:11:47

标签: javascript jquery

我有一些通过脚本分配的内嵌样式高度元素。我需要检查的是具有以下内容的元素: // Setting the button's listeners. endSessionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE); progressDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86")); progressDialog.setTitleText("Ending session..."); AlertDialog endDialog = new AlertDialog.Builder(getContext()).create(); endDialog.setTitle("End Session?"); Log.e("sessioncost", String.valueOf(session_cost)); endDialog.setButton(Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { progressDialog.show(); // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(getContext()); String url = "https://us-central1-something-something.cloudfunctions.net/endSession?bid=" + bid + "&sessioncost=" + session_cost; Log.e("end sesion button", url); // Request a string response from the provided URL. StringRequest endSessionRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(final String response) { progressDialog.dismiss(); Toast.makeText(getContext(), "Session Completed", Toast.LENGTH_LONG).show(); progressDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.SUCCESS_TYPE); progressDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86")); progressDialog.setTitleText("Session Completed: Bill"); progressDialog.setContentText("Please pay ?" + response + " to s."); progressDialog.setCancelable(false); progressDialog.show(); changeState('1'); bill_in_paise = Float.parseFloat(response) * 100; Log.e("bill", bill_in_paise.toString()); progressDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sweetAlertDialog) { sweetAlertDialog.dismiss(); Intent intent = new Intent(getContext(), PaymentActivity.class); intent.putExtra("amt", bill_in_paise.toString()); startActivityForResult(intent, REQUEST_CODE); } }); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show(); }// onErrorResnponse - END }); // Add the request to the RequestQueue. Cuz volley is asyc af. queue.add(endSessionRequest); // VOLLEY REQUEST - END } }); endDialog.setButton(Dialog.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getContext(), "Session not cancelled. " + which, Toast.LENGTH_LONG).show(); } }); endDialog.show(); } } }); // endSessionButton onclick - end

style="height: 0px"

jQuery

<div class="myEm" style="height: 30px">...</div>
<div class="myEm" style="height: 10px">...</div>
<div class="myEm" style="height: 0px">...</div>

https://jsfiddle.net/p9nfskeL/1/

3 个答案:

答案 0 :(得分:2)

以下作品:

$(".myEm").each(function() {
  if (this.style.height == "0px") {
    $(this).css("border-color", "red");
  }
});

答案 1 :(得分:1)

如果这是您所拥有的确切代码,并且您无法修改任何HTML,那么您可以在此处进行字符串比较。

$(".myEm").each(function() {
  if (this.style.height == '0px') {
    $(this).css("border-color", "red");
  }
});

问题是,您已将min-height设置为有效的10px,因此尽管您将0px定义为内联,但它不会覆盖CSS为{{1 }}是单独定义的。因此,您需要内联调用min-height或可以像上面那样检查。请注意,min-height: 0;将始终返回.height(),因此,我使用10来获取已声明为内联的值,而不是正在呈现的值。

Demo


请注意,如果HTML与您共享的完全一样,您也可以实现上述效果

this.style.height

Demo

答案 2 :(得分:1)

  1. 您的身高从不为0,因为您的CSS中有min-height: 10px
  2. 您可以直接使用$(this).height而不是访问样式对象。它给出一个数值,您可以如图所示进行NOT检查。

希望这会有所帮助!

$(".myEm").each(function() {
  console.log($(this).height())
  if (!$(this).height()) {
    $(this).css("border-color", "red");
  }
});
.myEm {
  border: 1px solid #ccc;
  line-height: 20px;
  margin: 20px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="myEm" style="height: 30px">...</div>
<div class="myEm" style="height: 10px">...</div>
<div class="myEm" style="height: 0px">...</div>