检查JavaScript中任何一行表格中是否存在类 [英] Check whether a class exists in any row of a table in JavaScript
本文介绍了检查JavaScript中任何一行表格中是否存在类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< table>
< tr onclick =selectRow>
< td>表数据< / td>
< td>表数据< / td> $
< / tr>
< tr class =selectedonclick =selectRow><
< td>表数据< / td>
< td>表数据< / td>
< / tr>
< / table>
< / code>< / pre>
class =selected
是点击行的结果。
我想创建一个按钮,当点击时,如果有一个类为'selected'的类会返回true,如果没有,则返回false。
任何帮助非常感谢。
Thanks。
解决方案
function checkExist(){
var myTr = document.getElementsByTagName('tr'); (myTr [i] .className.match('(^ | \\ s +)selected($ i
$ b for(var i = myTr.length; i - ;) \\s + | $)')){
alert('true'); // return true;
return;
}
}
alert('false'); //返回false
}
基本上附加到按钮或链接,如下所示:
< table>
< tr onclick =selectRow>
< td>表资料< / td>
< td>表资料< / td>
< td>表资料< / td>
< / tr>
< tr class ='selected'onclick =selectRow>
< td>表资料< / td>
< td>表资料< / td>
< td>表资料< / td>
< / tr>
< / table>
< a href ='#'onclick ='checkExist()'>点击我查找< / a>
I have a table (simplified for this question):
<table><br>
<tr onclick="selectRow"><br>
<td>table data</td><br>
<td>table data</td><br>
<td>table data</td><br>
</tr><br>
<tr class="selected" onclick="selectRow"><br>
<td>table data</td><br>
<td>table data</td><br>
<td>table data</td><br>
</tr><br>
</table>
class="selected"
is a result of clicking on the row.
I want to make a button that when clicked, would return true if there is a row with the class 'selected', and false if there is not.
Any help is greatly appreciated.
Thanks.
解决方案 function checkExist(){
var myTr = document.getElementsByTagName('tr');
for(var i=myTr.length; i--;){
if(myTr[i].className.match('(^|\\s+)selected(\\s+|$)')){
alert('true');//return true;
return;
}
}
alert('false'); //return false
}
and basically attach to a button or link like this:
<table>
<tr onclick="selectRow">
<td>table data</td>
<td>table data</td>
<td>table data</td>
</tr>
<tr class='selected' onclick="selectRow">
<td>table data</td>
<td>table data</td>
<td>table data</td>
</tr>
</table>
<a href='#' onclick='checkExist()'>click me to find out</a>
这篇关于检查JavaScript中任何一行表格中是否存在类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文