我有一个自动生成的表格,其中包含任务及其状态。每行都有一个按钮来重新启动任务。
如果任务的值不等于“错误”,我想隐藏重启按钮。所以换句话说:只有带有 Status==Error
的任务应该有一个可见的重启按钮
这是一个 fiddle 链接:https://jsfiddle.net/hwqt7c3a/5/
我试过:
window.onload = function () {
$(function () {
$('table tr').each(function () {
var Cells = this.getElementsByTagName("td");
if (Cells[2].innerText !== 'Error') {
$(this).find('button').style.visibility = 'hidden';
}
});
});
}
但由于某些原因,Cells
始终为空。
最佳答案
我已经更新了你的 fiddle ,这是 working fiddle
$(function() { //document ready event
$('table tr').each(function() { //loop all tr's
var Cell = $(this).find('td:eq(2)'); //find the 3rd td cell
if (Cell.text() !== 'Error') { //check if its text is "Error"
$(this).find('button').hide(); //if try then find the button of this tr and hide it
}
});
});
不要混合使用 javascript 和 Jquery 语法,
你也不需要这两行
window.onload = function () { // javascript way of handling document ready
$(function () { // jquery way of handling document ready.
使用任何一个,因为你正在使用 Jquery 库,那么你最好使用它
$(function () {
这样你就不会混淆语法..
如果您希望 td 占用 UI 中的按钮空间但仍被隐藏,那么您可以使用
$(this).find('button').css("visibility","none")
关于javascript - 在列的特定值上连续隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36836587/