【发布时间】:2017-07-11 06:40:37
【问题描述】:
我想在 jQuery 中更改表格的图标,但我无法找到元素并更改它的背景。我正在使用 jQuery 的树表库。
这是我的 jQuery 代码:
Factory.query().$promise.then(function(data) {
$rootScope.tree = data;
console.log($rootScope.tree);
buildTreeTable($rootScope.tree);
})
function buildTreeTable(tree){
$("#example").treetable({
expandable: true,
onNodeExpand: nodeExpand,
onNodeCollapse: nodeCollapse
});
function nodeExpand () {
getNode(this.id);
}
function nodeCollapse () {
console.log("Collapsed: " + this.id);
}
$("#example tbody").on("mousedown", "tr", function() {
$(".selected").not(this).removeClass("selected");
$(this).toggleClass("selected");
});
var len = tree.length;
for(var i = 0; i < len; i++){
if (tree[i].status == 1){
var print = $('#example').attr('class').find('a').css("background-image","url(../../images/image.png)");
console.log("Status is 1. Load icon for 1", print);
}
}
这是我的html:
<div class="panel-body">
<table id="example">
<tbody>
<thead>
<tr>
<th >Tree data</th>
<th>Header1</th>
<th>Header2</th>
</tr>
</thead>
</tbody>
</table>
</div>
我想根据从数据库中获取的内容更改<td> 的图标,这意味着如果状态为 1,我加载一张背景图片,如果状态为 2,我加载另一张。
我的<td> 元素包含<span> 和<a> 标签,我想更改<a> 标签的背景图片。我怎样才能在jQuery 中实现这一点?
这是页面加载后我的元素:
这是我得到的错误:
TypeError: $(...).attr(...).find is not a function
编辑:
这段代码是我尝试为每个 tr 加载单独图标的方式
function changeIcon(data){
for(var i = 0; i < data.length; i++){
if (data[i].status == 0){
$('tr span.icon').each(function(){
$(this).addClass('status');
})
console.log("Status is 0. Load icon for 0");
}else if(data[i].status == 1){
console.log("Status is 1. Load icon for 1");
}else if(data[i].status == 2){
console.log("Status is 2. Load icon for 2");
}else
console.log("Status is 3. Load icon for 3");
}
}
提前致谢!
【问题讨论】:
-
到目前为止您尝试过什么?请展示你的作品。说明您想要做什么以及正在发生的事情,以便我们帮助您解决问题。
-
我已经添加了我尝试过的内容。问题是背景图片没有添加到 标签中。
-
数据从数据库中动态加载。
-
您的 HTML 中没有
<a>标记。 -
我在页面加载后添加了一张图片。
标签: javascript jquery html css treetable