这是我的用户列表。
我需要更改红色圆形标签。
$("#infoListBox .label").text($("#infoListBox a").length);
$("#infoListBox .list").bind("DOMSubtreeModified",function(){
$("#infoListBox .label").text($("#infoListBox a").length);
});
当文档加载时,获取(红色)标签上的数字并更改标签;接下来,删除标签(将标签从 21 更改为 20)。
我想改进这段代码。
我的解决方案更好。
function getUserLength(){
return $("#infoListBox .label").text($("#infoListBox a").length);
}
// user count
getUserLength();
$("#infoListBox .list").bind("DOMSubtreeModified",function(){
$("#infoListBox .label").val(getUserLength());
});
我使用 jade 模板引擎,下面是我的 jade 代码..
div(id='infoListBox').ui.fluid.vertical.menu
div.header.item Users
a.ui.red.circular.label
div.list.item
a.item Me
a.item 조제우
a.item 장형주
a .item 남중민
a.item Me
a.item 조제우
a.item 장형주
a.item 남중민
a.item Me
a.item 조제우
最佳答案
我猜 $("#infoListBox .label")
代表一个 label
或 span
。所以使用 .text()
$("#infoListBox .label").text(getUserLength());
更新:以上也不需要,简单
$("#infoListBox .list").bind("DOMSubtreeModified",function(){
getUserLength();
});
由于getUserLength()
已经在红色圆形标签中显示了长度,所以只需在中调用此方法即可DOMSubtreeModified
事件。
关于javascript - 使用 jQuery 更改 div 标签的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20659670/