jQuery find()等价于javascript [英] jquery find() equivalent for javascript

查看:26
本文介绍了jQuery find()等价于javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表,我想检查一下它们是否包含特定元素,例如具有上一步"值的按钮.我通过使用jquery函数 find 并编写一个函数来解决它,但是我需要在没有jquery的情况下解决此问题.这可能吗?

I have three tables and i want to check wheter they contain a specific element, e.g. a button with the value 'Previous'. I solved it by using the jquery function find and writing a function, but i need to solve this problem without jquery. Is this possible?

var t1 = document.getElementById("table_one");
var t2 = document.getElementById("table_two");
var t3 = document.getElementById("table_three");

has_prev_button(t1);
has_prev_button(t2);
has_prev_button(t3);

function has_prev_button(element)
{
  var has_prev_button = false;
  var check = $(element).find("input[type=button]");
  
  for (i=0; i<=check.length-1; i++) {
    if (check[i].getAttribute("value") == "Previous") {
      has_prev_button = true;
    }
  }

  if (has_prev_button) {
    document.write("<p>The selected table has a Previous button</p>");
  } else {
    document.write("<strong><p style='color:red'>The selected table has NO Previous button</p></strong>");
  }
}

table {
  margin-bottom:40px;
  border: 1px solid black;
}

td {
  border: 2px solid #D8D8D8;
  width: 70px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_one">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_two">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_three">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

推荐答案

使用

var t1 = document.getElementById("table_one");
var t2 = document.getElementById("table_two");
var t3 = document.getElementById("table_three");

has_prev_button(t1);
has_prev_button(t2);
has_prev_button(t3);

function has_prev_button(element)
{
  var has_prev_button = false;
  var check = element.querySelectorAll("input[type=button]");
  
  for (i=0; i<=check.length-1; i++)
  {
    if (check[i].value == "Previous")
    {
      has_prev_button = true;
    }
  }

  if (has_prev_button)
  {
    document.write("<p>The selected table has a Previous button</p>");
  }
  else
  {
    document.write("<strong><p style='color:red'>The selected table has NO Previous button</p></strong>");
  }
}

table {
  margin-bottom:40px;
  border: 1px solid black;
}

td {
  border: 2px solid #D8D8D8;
  width: 70px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_one">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_two">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

<table id="table_three">
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  <tr>
  <tr>
    <td><input type="button" value="Previous"></td>
    <td><input type="button" value="Next"></td>
  </tr>
</table>

这篇关于jQuery find()等价于javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆