这就是我所拥有的。列表 Web 部件中的 SharePoint 2010 自定义 View 。我有 6 个类别和 4 个子类别。项目不必有子类别,但必须有类别。
该 View 显示一个空白子类别,旁边有一个数字。我试图将点击事件绑定(bind)到所有这些事件,但 ID 在每次页面刷新时都会增加。基本 ID 为 titl[0-9]*[0-9]。然后下面还有另一个我想检查的 ID,它是 titl[0-9]*_[0-9]1。
所以我尝试使用 regex selector for jQuery并且它没有正确绑定(bind)。它找到了对象,但未正确绑定(bind)。
我需要它绑定(bind)到id,然后能够触发下一个tbody(即1_)的onclick事件。然后检查它的文本是否为“”,如果是则隐藏 tbody。
我的代码:
$(":regex(id,titl[0-9]*-[0-9]_) td a").bind('click', function(){
var parent = $(this);
var child = $(this).next("tbody");
var grandchild = $(this).next("tbody td a");
//alert(parent + " | " + child + " | " + grandchild ); //always return undefined??
// Everything below works if I can get the IDs correct for child and grandchild
if($(grandchild).css('display')!='none'){
$(grandchild).click();
if($(grandchild).text()==" "){
$(child).hide();
};
};
});
最佳答案
我强烈建议您需要重新考虑您的 ID - 它们确实应该保持一致。
如果您绝对必须使用变量 ID,则可以像使用任何其他属性一样在选择器中使用“id”属性:
// Any element, ID starts with "titl"
$('[id^="titl"]')
为了捕获并重新使用它,我真的建议您对自己的 ID 做错了什么。然而,为了完整性(尽管我无法强调你应该尽量避免使用它),基于此的东西应该是一个很好的(哈哈,是的)起点
// Long-winded messy hideous foul code
var $title = $('[id^="titl"]'),
title = $title.length && $title.eq(0).attr('id');
if (title !== 0)
{
$('#' + title + ' #' + title + '1 td a').html('Ow.');
}
关于javascript - jQuery将点击事件绑定(bind)到每次页面刷新时数字不断增加的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904774/