jquery - 使用 jQuery 检查表是否具有属性值
问题描述
我正在尝试在窗口调整大小和加载时显示内容。之后单击table th
属性等于给定值。我不知道我在哪里做错了,但我无法得到结果。
$(window).on("load resize", function(e) {
var $theWindowSize = $(this).width();
if ($theWindowSize < 768) {
alert($theWindowSize);
if ($('table th').attr('id') == 'basic-tab') {
$('table th#basic-tab').on('click', function(e) {
alert('basic');
$('table .basic-content').css('display', 'block');
});
} else if ($('table th').attr('id') == 'standard-tab') {
$('table th#standard-tab').on('click', function(e) {
alert('standard');
$('table .standard-content').css('display', 'block');
});
}
}
});
.basic-content,
.standard-content {
dispaly: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th id="basic-tab">Tab1</th>
<th id="standard-tab">Tab2</th>
</tr>
<tr class="basic-content">
<td>Basic Content</td>
</tr>
<tr class="standard-content">
<td>Standard Content</td>
</tr>
</table>
解决方案
单击th
获取id
,将其拆分并获取第一部分,然后检查哪个tr
包含class
从此开始string
并包含content
。在上面应用 CSS
工作片段: -
$('table th').on('click',function(e){
var thId = $(this).attr('id');
var compare = thId.split('-')[0];
$('tr').each(function(){
if($(this).attr('class') != undefined){
$(this).css('display','none');
}
});
$('tr[class*='+compare+'-content]').css('display','block');
});
.basic-content,.standard-content{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th id="basic-tab">Tab1</th>
<th id="standard-tab">Tab2</th>
</tr>
<tr class="basic-content">
<td>Basic Content</td>
</tr>
<tr class="standard-content">
<td>Standard Content</td>
</tr>
</table>
注意:-根据您的 HTMLcontent
在代码中添加。$('tr[class*='+compare+'-content]')
如果您需要将 CSS 添加到所有包含basic
或standered
在其中的类中,请content
从代码中删除并制作它 $('tr[class*='+compare+'-]')
。
推荐阅读
- sql - SQL:使用元组构造函数的嵌套查询
- python - 如何在 Jupyter Notebook 中使用 Pycharm 来读取存储在 ram 中的对象?
- c++ - 在函数模板的显式特化声明中扣除尾随模板参数(无函数参数。扣除)
- c# - 如何使用 StructureMap 在构造函数中注入实现相同通用接口的所有类?
- python - 诗歌 - [AttributeError] 'bool' 对象没有属性 'strip'
- sql - 在两个表上使用 INNER JOIN 时,如何让它返回表中的每一列,
- android - 添加annotationProcessor时Android Studio抛出重复类错误
- java - 如何处理不同视图模型之间的通信?
- python - 基于所选日期的python数据框连接
- arrays - 如何在 Angular 外部访问从 API 订阅方法返回的数据