Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery将单击事件绑定到id,该id在每次页面刷新时的数目都会增加_Javascript_Jquery_Sharepoint - Fatal编程技术网

Javascript jQuery将单击事件绑定到id,该id在每次页面刷新时的数目都会增加

Javascript jQuery将单击事件绑定到id,该id在每次页面刷新时的数目都会增加,javascript,jquery,sharepoint,Javascript,Jquery,Sharepoint,这是我的。列表web部件中的SharePoint 2010自定义视图。我有6个类别和4个子类别。项目不必有子类别,但必须有一个类别 该视图显示一个空白子类别,旁边有一个数字。我试图将一个单击事件绑定到所有这些事件,但每次页面刷新时ID都会增加。基本ID为titl[0-9]*[0-9]。下面还有另一个我想检查的ID,它是titl[0-9]*.[0-9]1 因此,我尝试使用,但它不能正确绑定。它找到了对象,但没有正确绑定 我需要它绑定到id,然后能够触发下一个tbody的onclick事件,即1。然

这是我的。列表web部件中的SharePoint 2010自定义视图。我有6个类别和4个子类别。项目不必有子类别,但必须有一个类别

该视图显示一个空白子类别,旁边有一个数字。我试图将一个单击事件绑定到所有这些事件,但每次页面刷新时ID都会增加。基本ID为titl[0-9]*[0-9]。下面还有另一个我想检查的ID,它是titl[0-9]*.[0-9]1

因此,我尝试使用,但它不能正确绑定。它找到了对象,但没有正确绑定

我需要它绑定到id,然后能够触发下一个tbody的onclick事件,即1。然后检查其文本是否为“”,如果是,则隐藏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();
    };
  };
}); 

我不确定我是否了解这一点,但您可以以
titl
开头的任何ID为目标,然后在函数中以许多其他方式基于该ID进行过滤:

$('[id^="titl["]').on('click', function() {
    var check = this.id.charAt(10) == '_', //tenth character is underscore ?
        parent = $(this),
        child = $(this).next("tbody"),
        grandchild = $(this).next("tbody td a");

    if (check) {
        //element matching second selectortype clicked
    }else{
        if (grandchild.is(':visible')){
            grandchild.trigger('click');
            if (grandchild.text()==" ") child.hide();
        }
    }
});

我不确定我是否了解这一点,但您可以以
titl
开头的任何ID为目标,然后在函数中以许多其他方式基于该ID进行过滤:

$('[id^="titl["]').on('click', function() {
    var check = this.id.charAt(10) == '_', //tenth character is underscore ?
        parent = $(this),
        child = $(this).next("tbody"),
        grandchild = $(this).next("tbody td a");

    if (check) {
        //element matching second selectortype clicked
    }else{
        if (grandchild.is(':visible')){
            grandchild.trigger('click');
            if (grandchild.text()==" ") child.hide();
        }
    }
});

我强烈建议你需要重新考虑你的身份证-他们应该是一致的,真的

如果绝对必须使用变量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.');
}

我强烈建议你需要重新考虑你的身份证-他们应该是一致的,真的

如果绝对必须使用变量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.');
}

如果你能控制你的身份证,我同意重新考虑你的身份证。即使没有,StartsWith选择器也会获取所有较高级别的元素,您可以遍历到较低级别的元素。请记住,链接选择器意味着您可以匹配ID中类似的模式,而不必关注实际值


另一个注意事项是:我从来不需要借助正则表达式匹配jQuery。类似CSS3的选择器太强大了。

我同意如果你能控制你的ID,就重新考虑你的ID。即使没有,StartsWith选择器也会获取所有较高级别的元素,您可以遍历到较低级别的元素。请记住,链接选择器意味着您可以匹配ID中类似的模式,而不必关注实际值


另一个注意事项是:我从来不需要借助正则表达式匹配jQuery。类似CSS3的选择器太强大了。

为什么不给它一个可以引用的css类?为什么不给它一个可以引用的css类?它是SharePoint Web部件。我没办法用身份证做点什么。相信我,我希望有一个静态ID。这是一个SharePoint Web部件。我没办法用身份证做点什么。相信我,我希望有静态ID的.Marc,这是一个sharepoint web部件。通常我会编写一个新的web部件并使用您的SPServices,但是这次我没有这个选项。我必须使用列表视图web部件。Marc,它是sharepoint web部件。通常我会编写一个新的web部件并使用您的SPServices,但是这次我没有这个选项。我必须使用列表视图web部件。